473,769 Members | 7,320 Online
Bytes | Software Development & Data Engineering Community
+ 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 3795
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
3919
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 column table names. I know that there are codes to generate tabs and carriage returns, but can't find anything about including "commas" in a string to output to the file to separate the custom field names. I'd appreciate some help with a line of...
2
2005
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 an ID. Currently: ID = r"\w+(?!)" FILENAME = r"(:)?+\.((mls)|(o))"
2
13500
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 background so my approach may not be the best way to tackle this. However, from the research I have done this approach seems reasonable. Also, I know about the undocumented procedure sp_MSforeachtable. That can give me a
0
1452
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 XercesDOMParser; try
0
1438
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 = XML::Xerces::LocalFileInputSource->new("http://www.foo.com/foo.xml");
5
1998
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 of four. I can get my script to work to display the images but I can't figure out how to do two things: 1. How do I get only one image into each table data entry? 2. How do I get it to create blank table data entries for any amount less than 4...
1
1728
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. The problem I have today and it is the same in the PHAR package, is that I don't know how to restrict the PHP parser to the bootstrap code of the package file. I explain : As in the shar format, the idea is to put some bootstrap code first,...
1
1380
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 new lines in the output. Help me avoid this problem. use XML::Parser; my $parser = XML::Parser->new( Handlers => { Init => \&handle_doc_start,
5
1991
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 following html: <form method="POST" enctype="multipart/form-data" name="image_upload_form" action=""> <p><input type="file" name="image_file" size="20"></p> <p><input type="submit" value="Upload Image" name="action"></p> </form> I'm dealing...
0
10216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5309
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3565
muto222
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.