Basic doubt about XML file generation
Hi,
I am new to Java. I am facing some trouble designing and implementing a particular problem involving XML files.
Problem:
I have an XML file which contains a set of properties and their default value. What I need to do is to read another file/stream which will have name=value strings and then, for all the names that I find there, change default value to current value. These names can appear in any order. For example,
<name1 value=default1/>
<name2 value=default2/>
<name3 value=default3/>
And I will get something like "set name3=value3 name1=value1" (This data comes to me from a stream -usb device- and not from a file). Then I should change it to
<name1 value=value1/>
<name2 value=default2/>
<name3 value=value3/>
What is the best possible way to do this in Java? This has to go into an embedded system so it will be nice if it can be done with minimum memory and less processing power.
I am currently reading J2EEtutorial (1.4). I have not read much. But I am confused if I should be using SAX, DOM or XSLT. To speak the truth, I don't even know the difference between all those. Any help will be greatly appreciated.
Thanks & Regards,
Suseelan
[1291 byte] By [
sbs_iitma] at [2007-11-27 8:06:42]

Use a DOM parser to parse the original XML.
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document document = domBuilder.parse( inputStreamToFile );
Then read in the contents from your USB stream into say an array or Vector. Then go through each child node in the domBuilder class, get the nodes name and see if it's in the Vector of objects from the USB stream. If it is, then replace the "Value" attribute in the dom node with what is in the Vector.
Make sense?
Message was edited by:
bryano
A J2EE tutorial doesn't sound like the right thing to read for an embedded device, unless you are just looking at the parts related to XML.
If you really want to minimize the memory usage (I don't know how important that is and I don't know how big your XML file is, so maybe it doesn't matter all that much) then SAX is a better approach. You can do what you described there with a SAX filter. Here's a link to an XML book that covers that topic quite well:
http://www.cafeconleche.org/books/xmljava/chapters/index.html
However if you're working with a J2ME system then your XML options may differ from the standard J2SE system.
Thanks for all the replies.
I evaluated all the options that I had and decided to use DOM. (I was suggested another two options - SAX & Castor). SAX I rejected since I was not really interested in parsing XML data. Castor was rejected since it needed strings (Actual i/p to my program) to be mapped to other data types.
However, I really liked the concept behind castor (castor.org).
Also, thanks for the XML book link.