dcsimg
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: Java
Expertise: Beginner
Apr 4, 2018

WEBINAR:

On-Demand

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


Assembling XML with String Operations

String name = …

String attribute = …

String xml = "<root>"+"<name attribute=\""+ attribute +"\">"+ name +"</name>"+</root>";

Many beginners are tempted to create an XML file like the one listed above using String operations because it is easier, but this approach fails to escape reserved characters. If one of the variable name or attribute contain any of the reserved characters <,>,&, " or ' the result would be invalid.

The solution would be that the XML should be assembled in a DOM, using the JDom library, like this:

Element root = new Element("root");

Root.setAttribute("attribute", attribute);

Root.setText(name);

Document doc = new Document();

doc.setRootElement(root);

XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());

String xml = out.outputString(root);
Octavia Anghel
 
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