Re: [dom4j-user] Namespaces become blank when inserting or updating elements
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:
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
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:
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
I read the XML file into a DOM4J Document:
String NS = "urn:bogus-namespace";
Map namespaces = new HashMap();
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:
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
So I added the namespace back in on the root element and tried it again:
d.getRootElement().addNamespace( "", NS );
Which manifested the problem:
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
VALUE
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
Re: [dom4j-user] Namespaces become blank when inserting or updating elements
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: VALUE VALUE VALUE VALUE VALUE VALUE VALUE I read the XML file into a DOM4J Document: String NS = "urn:bogus-namespace"; Map namespaces = new HashMap(); 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: VALUE VALUE VALUE VALUE VALUE VALUE VALUE So I added the namespace back in on the root element and tried it again: d.getRootElement().addNamespace( "", NS ); Which manifested the problem: VALUE VALUE VALUE VALUE VALUE VALUE VALUE 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 [email protected] https://lists.sourceforge.net/lists/listinfo/dom4j-user
Re: [dom4j-user] Namespaces become blank when inserting or updating elements
Are you creating the problem elements with code or are they in your
loaded document?
Jon Brisbin wrote:
> Searched in the archives and didn't find what I was looking for (but not
> really sure what I'm looking for, so don't throw things at me if this
> has been asked before).
>
> I have an XML template that I read into a DOM4J (using 1.6) document
> when I begin processing. The default namespace is set in the document
> and I set it again, just for good measure:
>
> SAXParser p = SAXParserFactory.newInstance().newSAXParser();
>
> DocumentFactory df = DocumentFactory.getInstance();
> df.setXPathNamespaceURIs( namespaces );
>
> SAXContentHandler ch = new SAXContentHandler();
> p.parse( ClassLoader.getSystemResourceAsStream( TEMPLATE_FILE ), ch );
>
> Document doc = ch.getDocument();
> doc.getRootElement().addNamespace( "", EFILE_NS );
>
>
> When I process the raw data, I do XPath lookups on nodes in the template
> and set them like so:
>
> XPath xp = root.createXPath( xpath );
> xp.setNamespaceURIs( namespaces );
> Element e = (Element) xp.selectSingleNode( root );
> if ( e != null ) {
> e.setText( value );
> return e;
> } else {
> return root;
> }
>
> But when the file is generated, some of the elements below the root
> element (but not all of them) have namespaces set to '' (blank). This
> causes the other party's XML processing to blow up with the error that
> the element with the blank namespace isn't in the E-file namespace, like
> it's supposed to be.
>
> If the namespace is set in the template, then I would expect it work
> correctly when I've read that document in and set the namespaces
> accordingly. That's not happening and some elements in the DOM are being
> "taken out" of the default namespace.
>
> Can someone shed some light on why this is happening?
>
>
-
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
