473,472 Members | 2,247 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SAX parser result to XML file

33 New Member
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
5 3775
mharrison
26 New Member
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
baskarpr
33 New Member
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
r035198x
13,262 MVP
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
baskarpr
33 New Member
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
r035198x
13,262 MVP
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

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

Similar topics

2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL...
2
by: Miki Tebeka | last post by:
Hello All, In a configuration file there can be ID's and filename tokens. The file names have a known suffix (.o or .mls) and I need to get a regular expression that will catch filename but not...
2
by: Bob | last post by:
Everybody, I've been doing a lot of on-line research and cannot find any reference to the exact problem I'm having. Let me preface this question with the fact that I'm coming from an Oracle...
0
by: Greg O'Rawe | last post by:
Hi, I am using the Xerces C++ parser API (2.3.0) on Solaris 2.8 and am getting a problem if I try to parse a file which does not exist. Here's the code: XercesDOMParser *parser = new...
0
by: Francesco Moi | last post by:
Hi. I'm trying to parse a HTTP file http://www.foo.com/foo.xml by using Xerces-Perl: ------------------------------------------ use XML::Xerces; my $file =...
5
by: JackM | last post by:
I need a little guidance putting a script together. I'm trying to read a list of image links from a text file (not a database) and display them in a table on my page. I want to display them in rows...
1
by: flconseil | last post by:
Hello, I am writing a tool to package several files into a single file. It is quite similar to the 'phar' PEAR package but with more features, as I want to connect it with an autoload mechanism....
1
by: reddyth | last post by:
Dear All, I wanted to parse an XML file and print the element's content. I have the following code for the same. I have printed the ourput too. The problem is it is printing unwanted spaces and...
5
by: LELE7 | last post by:
I'm new to PHP, but I usually program Windows Apps in .NET(c#, VB). So please excuse my unfamiliarity with code and HTML combined. It seems pretty simple to upload a file to a website using the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.