I produced this test from the example: http://www.dom4j.org/guide.html
Maybe something with how you're loading the document is causing your
problems.
import java.net.URL;
import java.io.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Test {
public static void main(String[] args) throws Exception{
Document doc = parse(args[0]);
write(doc);
}
public static Document parse(String filename) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read( new FileReader(filename) );
return document;
}
public static void write(Document document) throws Exception {
// Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint();
writer = new XMLWriter( System.out, format );
writer.write( document );
}
}
I ran it on your XML and it printed this to standard out:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:bogus-namespace">
<first-level>
<second-level>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
</second-level>
<second-level>
<third-level>VALUE</third-level>
<third-level>
<data>VALUE</data>
<data>VALUE</data>
</third-level>
</second-level>
</first-level>
</root>
Also, when you have a DOM4J document in memory, it doesn't have the
concept of inheriting namespaces. Every node has a namespace, so if
you change the namespace of one node, none of its descendants are
influenced.
--Evan
Jon Brisbin wrote:
Evan Kirkconnell wrote:
Are you creating the problem elements with code or are they in your
loaded document?
The problem elements are the direct descendants of the element I add the
namespace to.
I created a test case to see what's going on. It seems to be related to
the SAXContentHandler creating the DOM4J Document for me.
When I create the DOM4J document by reading in an XML file using the
SAXContentHandler, the default namespace gets stripped off. Here's the
file I read in:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:bogus-namespace">
<first-level>
<second-level>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
</second-level>
<second-level>
<third-level>VALUE</third-level>
<third-level>
<data>VALUE</data>
<data>VALUE</data>
</third-level>
</second-level>
</first-level>
</root>
I read the XML file into a DOM4J Document:
String NS = "urn:bogus-namespace";
Map<String, String> namespaces = new HashMap<String, String>();
namespaces.put( "", NS );
DocumentFactory df = DocumentFactory.getInstance();
df.setXPathNamespaceURIs( namespaces );
SAXContentHandler ch = new SAXContentHandler();
p.parse( ClassLoader.getSystemResourceAsStream( "dom4j-test.xml" ), ch );
Document d = ch.getDocument();
System.out.println( d.asXML() );
When I printed out the document, the default namespace gets stripped off:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<first-level>
<second-level>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
</second-level>
<second-level>
<third-level>VALUE</third-level>
<third-level>
<data>VALUE</data>
<data>VALUE</data>
</third-level>
</second-level>
</first-level>
</root>
So I added the namespace back in on the root element and tried it again:
d.getRootElement().addNamespace( "", NS );
Which manifested the problem:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="urn:bogus-namespace">
<first-level xmlns="">
<second-level>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
<data>VALUE</data>
</second-level>
<second-level>
<third-level>VALUE</third-level>
<third-level>
<data>VALUE</data>
<data>VALUE</data>
</third-level>
</second-level>
</first-level>
</root>
When I read the document in using the internal Java 1.5 Xerces parser,
then used the DOM4J DOMReader to create a Document out of the XML file,
everything works as expected and the default namespace is preserved like
I expect.
I've since changed my application to use a DOMReader and have abandoned
the SAXContentHandler as a method for creating DOM4J Documents from
files. Using a W3C DOM first seems a little unnecessary, but if that's
what I've got to do...
|
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
dom4j-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/dom4j-user