Java xml basic questions

R

Rolf.Kemper

Dear Java experts,

I'm Java beginner , but xml experienced.
I'm working with:

java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

or later version.

I would like to write a dom tree to an xml file.
If possible I would like to avoid additional installation of Java
classes.

Is the following class not is the standard distribution ?

import org.apache.xml.serialize.XMLSerializer;

I get package org.apache.xml.serialize does not exist !

What could be wrong ?
If it can not work without installation of new classes, what is the
best option ?

Thanks a lot

Rolf
 
D

Daniel Pitts

Dear Java experts,

I'm Java beginner , but xml experienced.
I'm working with:

java version "1.5.0_11"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_11-b03)
Java HotSpot(TM) Client VM (build 1.5.0_11-b03, mixed mode, sharing)

or later version.

I would like to write a dom tree to an xml file.
If possible I would like to avoid additional installation of Java
classes.

Is the following class not is the standard distribution ?

import org.apache.xml.serialize.XMLSerializer;

I get package org.apache.xml.serialize does not exist !

What could be wrong ?
If it can not work without installation of new classes, what is the
best option ?

Thanks a lot

Rolf

org.apache classes probably come from apache.org. I'm guessing thats
part of Xerces, but I don't know for certain... It is not standard,
but easy to bundle.
 
S

Steve W. Jackson

Daniel Pitts said:
org.apache classes probably come from apache.org. I'm guessing thats
part of Xerces, but I don't know for certain... It is not standard,
but easy to bundle.

The Apache Xerces and Xalan classes are bundled as of 1.5. They're
simply moved to another package hierarchy in order to allow users the
ability to use newer releases from Apache should they wish.

But they're not all listed in the Javadocs, including XMLSerializer.
It's now at com.sun.org.apache.xml.internal.serialize.XMLSerializer.
Note the "com.sun" prefix and the addition of "internal". Poke around
in the src.zip with the JDK installation.

= Steve =
 
R

Rolf.Kemper

org.apache classes probably come from apache.org. I'm guessing thats
part of Xerces, but I don't know for certain... It is not standard,
but easy to bundle.- Zitierten Text ausblenden -

- Zitierten Text anzeigen -

Daniel,
thanks for your mail. Based on the little I know your understanding is
right. I think XERCES is part of standard distribution.
This is why I'm a bit pussled that XMLSerializer is obviously not.
Meantime I found a solution by using TransformerFactory and DomSource.
But I have not finalized it yet.

The part I'struggling is the following:

I have created a Nodelist out of an XML docuemnt by Xpath which works
well.
// snipet
NodeList Config = (NodeList )xpath.evaluate("/ShotMapConfigs/
Config[@Name='" + "uCFL_V1.0" + "']", DocConfigXML ,
XPathConstants.NODESET )
// end snipet

Next I tried to add a single node out of this list to a NEW dom
document.
I get an error that the nodes are not from the same document.
Basically I undestand that.
I tried to use DocumentFragment , but this does not help.
// snipet
Document doc = impl.createDocument(null,"ShotMapDocLog", null);
DocumentFragment fragment = doc.createDocumentFragment();
fragment.appendChild(Config.item(0));
Element root = doc.getDocumentElement();
// end snipet

So , the question on this is how can I let's say copy a fragment from
one dom to another ?
Any idea is welcome.

Rolf
 
W

wisccal

On May 1, 7:52 am, (e-mail address removed) wrote:
org.apache classes probably come from apache.org. I'm guessing thats
part of Xerces, but I don't know for certain... It is not standard,
but easy to bundle.- Zitierten Text ausblenden -
- Zitierten Text anzeigen -

Daniel,
thanks for your mail. Based on the little I know your understanding is
right. I think XERCES is part of standard distribution.
This is why I'm a bit pussled that XMLSerializer is obviously not.
Meantime I found a solution by using TransformerFactory and DomSource.
But I have not finalized it yet.

The part I'struggling is the following:

I have created a Nodelist out of an XML docuemnt by Xpath which works
well.
// snipet
NodeList Config = (NodeList )xpath.evaluate("/ShotMapConfigs/
Config[@Name='" + "uCFL_V1.0" + "']", DocConfigXML ,
XPathConstants.NODESET )
// end snipet

Next I tried to add a single node out of this list to a NEW dom
document.
I get an error that the nodes are not from the same document.
Basically I undestand that.
I tried to use DocumentFragment , but this does not help.
// snipet
Document doc = impl.createDocument(null,"ShotMapDocLog", null);
DocumentFragment fragment = doc.createDocumentFragment();
fragment.appendChild(Config.item(0));
Element root = doc.getDocumentElement();
// end snipet

So , the question on this is how can I let's say copy a fragment from
one dom to another ?
Any idea is welcome.

Rolf

I believe all that you have to do is to import the node first, which
creates a copy that can be inserted into the new Document. Something
like:

Node node = doc.importNode(Config.item(0), true); //true to deep-copy
....
fragment.appendChild(node);

Also, I would recommend that you use the various factory classes
instead of the vendor-specific implementations. I.e., instead of
impl.createDocument(), use DocumentBuilderFactory & DocumentBuilder to
parse or to create a new Document.

Hope this proves helpful.

Steve
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top