(Tutorial) XML generation with JAVA
Tutorial : XML generation with JAVA
XML developers used to rely on XML parsers to read XML files. They also used to rely on XML processors to transform XML to *ML (HTML, XML ...). However, most of them forget these tools to generate XML from scratch. They should not ...
Below, the XML file you want to generate from input data. It's just a list of user. Each user has an ID a TYPE and a NAME.
An XML novice developer could write the following code to quickly generate users.xml :
(1) - Serialization to file output stream -
[...]
String ENCODING = "ISO-8859-1";
String[] id = {"PWD122","MX787","A4Q45"};
String[] type = {"customer","manager","employee"};
String[] desc = {"Tim@Home","Jack&Moud","John D'oé"};
PrintWriter out = new PrintWriter(new FileOutputStream("users.xml"));
out.println("<?xml version=\"1.0\" encoding=\""+ENCODING+"\"?>");
out.println("<!DOCTYPE USERS SYSTEM \"users.dtd\">");
out.println("<USERS>");
for (int i=0;i<id.length;i++)
{
out.println("<USER ID=\""+id[i]+"\" TYPE=\""+type[i]+"\">"+desc[i]+"</USER>");
}
out.println("</USERS>");
[...]
Courtesy:- Javazoom.net
- guru's blog
- Login to post comments
