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). -
TransformerFactory trf = TransformerFactory.newInstance();
-
Transformer tr = trf.newTransformer();
-
DOMSource domSource = new DOMSource(doc); //Document obj, result of parsing is used to create new XML file.
-
StreamResult streamResult = new StreamResult("resulted.xml");
-
tr.transform(domSource, streamResult);
-
Below I simply tried for SAX parsing. But you can notice there is no relationship b/w parsing result and new XML file. -
SAXParserFactory spf1 = SAXParserFactory.newInstance();
-
SAXParser sp1 = spf1.newSAXParser();
-
sp1.parse(new File("original.xml"), handler1);
-
-
InputSource is1 = new InputSource("original.xml");
-
SAXSource saxsrc1 = new SAXSource(is1);
-
TransformerFactory trf1 = TransformerFactory.newInstance();
-
Transformer tr1 = trf1.newTransformer();
-
tr1.transform(saxsrc1, new StreamResult("resulted.xml"));
-
Thanks in advance.
5 3795
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?
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.
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? - try {
-
String file = "test1.xml";
-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-
DocumentBuilder builder = factory.newDocumentBuilder();
-
Document document = builder.parse(new File(file));
-
Element root = document.getDocumentElement();
-
System.out.println(root.getTagName());
-
System.out.println(root.getAttribute("name"));//If the root contains the attribute name
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
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? - try {
-
String file = "test1.xml";
-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-
DocumentBuilder builder = factory.newDocumentBuilder();
-
Document document = builder.parse(new File(file));
-
Element root = document.getDocumentElement();
-
System.out.println(root.getTagName());
-
System.out.println(root.getAttribute("name"));//If the root contains the attribute name
-
}
-
catch(Exception e) {
-
e.printStackTrace();
-
}
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 ?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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))"
|
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
|
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
|
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");
| |
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...
|
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,...
|
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,
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |