By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,797 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,797 IT Pros & Developers. It's quick & easy.

SAX parser result to XML file

P: 33
Hi all,
I my program after parsing in SAX parser, I want to write the parse result as an XML file. I want to ensure that there should be no difference between source XML file and parse result xml file. Because I set some properties in parser, which may cause to changes between actual and parsed.

What I expect is the exact XML file structure is to be available into another XML file (incl white spc's) after SAX parsing.

Below is a snippet to convert a Document object (result of parsing).
Expand|Select|Wrap|Line Numbers
  1. TransformerFactory trf = TransformerFactory.newInstance();
  2. Transformer tr = trf.newTransformer();
  3. DOMSource domSource = new DOMSource(doc); //Document obj, result of parsing is used to create new XML file.
  4. StreamResult streamResult = new StreamResult("resulted.xml");
  5. tr.transform(domSource, streamResult);
  6.  
Below I simply tried for SAX parsing. But you can notice there is no relationship b/w parsing result and new XML file.

Expand|Select|Wrap|Line Numbers
  1. SAXParserFactory spf1 = SAXParserFactory.newInstance();
  2. SAXParser sp1 = spf1.newSAXParser();
  3. sp1.parse(new File("original.xml"), handler1);
  4.  
  5. InputSource is1 = new InputSource("original.xml");
  6. SAXSource saxsrc1 = new SAXSource(is1);
  7. TransformerFactory trf1 = TransformerFactory.newInstance();
  8. Transformer tr1 = trf1.newTransformer();
  9. tr1.transform(saxsrc1, new StreamResult("resulted.xml"));
  10.  
Thanks in advance.
Jan 11 '07 #1
Share this Question
Share on Google+
5 Replies


P: 26
Lets say for example you had the following XML file:

<root>

<list>

<item>

<name>Carrots</name>

</item>

</list>

</root>

What are you doing with this file - anything? e.g. would you change Carrots to Oranges? What I am getting at is does your program modify the XML file before it is written out?
Jan 17 '07 #2

P: 33
Lets say for example you had the following XML file:
<root>
<list>
<item>
<name>Carrots</name>
</item>
</list>
</root>

What are you doing with this file - anything? e.g. would you change Carrots to Oranges? What I am getting at is does your program modify the XML file before it is written out?

Sometimes it may happen. Like some elements/attrib's can be added. Though it is not modified, I want the parsed result should be written in file. In one case, I want to compare the actual XML (before parsing) and parse result xml file (which written in a new file, after parsing). So, before comparison parsed content is to be retrieved and written into a file.
Jan 23 '07 #3

10K+
P: 13,264
Sometimes it may happen. Like some elements/attrib's can be added. Though it is not modified, I want the parsed result should be written in file. In one case, I want to compare the actual XML (before parsing) and parse result xml file (which written in a new file, after parsing). So, before comparison parsed content is to be retrieved and written into a file.
Well this is how to parse an XML file using the DOM approach. Do you want to create another XML file which is an edited version of the one you have?

Expand|Select|Wrap|Line Numbers
  1. try {
  2.     String file = "test1.xml";
  3.     DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
  4.     DocumentBuilder builder = factory.newDocumentBuilder();
  5.     Document document = builder.parse(new File(file));
  6.     Element root = document.getDocumentElement();
  7.     System.out.println(root.getTagName());
  8.     System.out.println(root.getAttribute("name"));//If the root contains the attribute name
  9. }
  10. catch(Exception e) {
  11.     e.printStackTrace();
  12. }
Jan 23 '07 #4

P: 33
Well this is how to parse an XML file using the DOM approach. Do you want to create another XML file which is an edited version of the one you have?

Expand|Select|Wrap|Line Numbers
  1. try {
  2.     String file = "test1.xml";
  3.     DocumentBuilderFactory factory =  DocumentBuilderFactory.newInstance();
  4.     DocumentBuilder builder = factory.newDocumentBuilder();
  5.     Document document = builder.parse(new File(file));
  6.     Element root = document.getDocumentElement();
  7.     System.out.println(root.getTagName());
  8.     System.out.println(root.getAttribute("name"));//If the root contains the attribute name
  9. }
  10. catch(Exception e) {
  11.     e.printStackTrace();
  12. }


ya, this works well with DOM, in the same way i would like to do with SAX. that is i would like to write the SAX parsed content into a file (using SAXsource). Any idea ?
Jan 25 '07 #5

10K+
P: 13,264
ya, this works well with DOM, in the same way i would like to do with SAX. that is i would like to write the SAX parsed content into a file (using SAXsource). Any idea ?
Oh yeah. I answered too quickly there. Forgot about the title. Here is a chapter on using both methods.
Jan 25 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.