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...

-- 


Thanks!

Jon Brisbin
Portal Webmaster
NPC International, Inc.
http://www.npcinternational.com



-------------------------------------------------------------------------
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
dom4j-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to