Creating the XML file from the dom Document

B

Bob

Hi,

I used the standard API to create an "org.w3c.dom.Document" Object from
an XML file(using a DocumentBuilder).

Now I want to use the Document Object to create (print) the XML file
(i.e. the reverse process).

Does anyone know of anything that does this for you or do you have to
do it manually? (I'm pretty sure there should be something out there,
as this is a pretty standard procedure)

Regards,
Bob
 
U

Ulf Nordlund

Bob skrev:
Hi,

I used the standard API to create an "org.w3c.dom.Document" Object from
an XML file(using a DocumentBuilder).

Now I want to use the Document Object to create (print) the XML file
(i.e. the reverse process).

Does anyone know of anything that does this for you or do you have to
do it manually? (I'm pretty sure there should be something out there,
as this is a pretty standard procedure)

Regards,
Bob

I'm using Apaches stuff. The code looks like this ('doc' is the DOM
document):

**************************

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
..
..
..
public void save(PrintWriter pOut) throws IOException {
OutputFormat of = new OutputFormat(doc);
of.setIndent(4);
of.setLineSeparator("\n");
XMLSerializer xs = new XMLSerializer(pOut,of);
xs.serialize(doc);
}

*************************

One drawback: The above will sort your attributes alphabetically, and
there doesn't seem to be any way of specifying the order of the
attributes yourself... (or is there? anyone?)
/ulf
 
A

Alan Gutierrez

Bob skrev:

I'm using Apaches stuff. The code looks like this ('doc' is the DOM
document):

**************************

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
.
.
.
public void save(PrintWriter pOut) throws IOException {
OutputFormat of = new OutputFormat(doc);
of.setIndent(4);
of.setLineSeparator("\n");
XMLSerializer xs = new XMLSerializer(pOut,of);
xs.serialize(doc);
}

*************************

One drawback: The above will sort your attributes alphabetically, and
there doesn't seem to be any way of specifying the order of the
attributes yourself... (or is there? anyone?)

Use a JAXP serializer. I'm using Saxon 8.0. It outputs
attributes in document order.

http://xml.apache.org/xerces2-j/faq-general.html#faq-5

The Trax implementation that comes with JDK 1.4 is brokenish.
You'll want to use a newer Xerces or Saxon.
 
S

Steve W. Jackson

Alan Gutierrez said:
Use a JAXP serializer. I'm using Saxon 8.0. It outputs
attributes in document order.

http://xml.apache.org/xerces2-j/faq-general.html#faq-5

The Trax implementation that comes with JDK 1.4 is brokenish.
You'll want to use a newer Xerces or Saxon.

We've been using Xerces and this means of serializing for well over 3
years, and until seeing this I had never given any thought to the order
in which the attributes were output. But an obvious question is why it
matters.

= Steve =
 
U

Ulf Nordlund

Steve W. Jackson skrev:
We've been using Xerces and this means of serializing for well over 3
years, and until seeing this I had never given any thought to the order
in which the attributes were output. But an obvious question is why it
matters.

= Steve =

It matters if you have several attributes and you want to be able to
easily read the resulting XML file (during development, for instance).
It would make it easier to find a particular attribute. No other reason..
/ulf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top