Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Manipulate XML Content the Ximple Way  : Page 7

For many common use cases, you can improve your XML-processing performance by taking advantage of XML-VTD's document-centric processing model.


advertisement
Example 4: Erasing Fields
This last example erases the fields of the second "CD" element, highlighted in the output XML. Notice that this approach works much better than removing the second element, because you can later fill in new values.

 Input XML

Output XML



<CATALOG>
  <CD>
    <TITLE>Empire Burlesque      </TITLE>
    <ARTIST>Bob Dylan     </ARTIST>
    <COUNTRY>USA    </COUNTRY>
    <COMPANY>Columbia      </COMPANY>
    <PRICE>10.9    </PRICE>
    <YEAR>1985    </YEAR>
  </CD>
  <CD>
    <TITLE>Still Got the Blues   </TITLE>
    <ARTIST>Gary More     </ARTIST>
    <COUNTRY>UK     </COUNTRY>
    <COMPANY>Virgin </COMPANY>
    <PRICE>10.2    </PRICE>
    <YEAR>1990    </YEAR>
  </CD>
</CATALOG> 

<CATALOG>
  <CD>
    <TITLE>Empire Burlesque      </TITLE>
    <ARTIST>Bob Dylan     </ARTIST>
    <COUNTRY>USA    </COUNTRY>
    <COMPANY>Columbia      </COMPANY>
    <PRICE>10.9    </PRICE>
    <YEAR>1985    </YEAR>
  </CD>
  <CD>
    <TITLE>                      </TITLE>
    <ARTIST>              </ARTIST>
    <COUNTRY>       </COUNTRY>
    <COMPANY>              </COMPANY>
    <PRICE>        </PRICE>
    <YEAR>        </YEAR>
  </CD>
</CATALOG>

And here's the code:

import com.ximpleware.*; import java.io.*; public class erase { public static void main(String[] args) throws Exception{ VTDGen vg = new VTDGen(); AutoPilot ap = new AutoPilot(); ap.selectXPath("/CATALOG/CD[PRICE=10.2]/*/text()"); if (vg.parseFile("old_cd.xml",false)){ VTDNav vn = vg.getNav(); ap.bind(vn); int i; byte[] ba = "".getBytes(); while((i=ap.evalXPath())!=-1){ vn.overWrite(i,ba); } FileOutputStream fos = new FileOutputStream("new_cd.xml"); fos.write(vn.getXML().getBytes()); fos.close(); } } }

At this point, you've seen how VTD-XML functions as a slicer, an editor, and an incremental modifier to help manipulate your XML documents intelligently. Most of the features discussed in this article are available to you only through VTD-XML. As XML assumes an increasingly larger role in enterprise computing, it's also simplifying the enterprise IT infrastructure by breaking down silos. Many of XML's much-discussed performance issues are not caused by XML per se, but instead by excessive, un-moderated use of OO design methodology. The power of non-extractive, "document-centric" XML processing takes a balanced approach and aims to fundamentally resolve those problems. But that is just a new beginning; the best is yet to come. By judiciously combining the various new tools and features, you can discover new approaches and develop new ways of thinking that is tailored toward a services-oriented, "smart-data" world.



Jimmy Zhang is a co-founder of XimpleWare, a provider of high-performance XML processing solutions. He has experience in the fields of electronic design automation and Voice-over IP, having worked for a number of Silicon Valley high-tech companies. He graduated from UC Berkeley with an MS and a BS from the department of EECS.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap