dcsimg
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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: XML
Expertise: Beginner
Apr 1, 2008

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


The Easiest and Most Efficient Way to Modify XML Documents

The easiest most and most efficient way to update XML documents is to use non-extractive parsers (such as VTD-XML) because they support incremental content update. Suppose you have the following XML document, named test.xml. Below is a snippet that inserts two attribute name/value pairs into an XML document.

  <tag> hello world </tag>
Now, here's the Java code for doing the attribute insertion:

public class simpleApp2 {
   public static void main(String[] s) throws Exception {
      VTDGen vg = new VTDGen();
      XMLModifier xm = new XMLModifier();
      if (vg.parseFile("test.xml", false)){
             VTDNav vn = vg.getNav();
             xm.bind(vn);
             xm.insertAttribute(" attr1='val1" attr2='val2'");
             xm.output("new.xml");
     }
  }   
}
The output, named new.xml, is shown below:

 <tag attr1='val1" attr2='val2'> hello world </tag>
Jimmy Zhang
 
Comment and Contribute

 

 

 

 

 


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

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date