Hi,

I encountered an error trying to convert an org.w3c.dom.Document to an
org.dom4j.Document. Consider the following XML document:

<xmltest xmlns="http://www.aaa.com"; xmlns:nsa="http://www.aaa.com";
xmlns:nsb="http://www.bbb.com";>
  <elema>aaaaa</elema>
  <nsa:elemA>AAAAA</nsa:elemA>
  <nsb:elemb>bbbbb</nsb:elemb>
</xmltest>

It has a default namespace ("http://www.aaa.com";). The prefix "nsa" is
associated with this namespace too. I need to get such a document as an
org.w3c.dom.Document. So I parsed the file containing the above document
into an org.w3c.dom.Document using namespace awareness. Than, I converted it
into an org.dom4j.Document and tried to access the elements through XPath
expressions. But only the <nsb:elemb> element could be accessed.

Parsing the document with namespace awareness off and converting it to dom4j
created elements such as
<nsb:nsb:elemb>, i.e. "nsb" as the prefix and "nsb:elemb" as the local name.

Does anybody have an idea how to solve this problem?

Igor



Take a look at the java code and it's output comparing the same document
parsed with W3C DOM Parser and with the SAXReader:

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.DOMReader;

public class XpathTest {
    public static void main(String[] args) {
        Element elem;

        File file = new File("C:\\xmltest\\xmltest.xml");

        try {
            DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            org.w3c.dom.Document domdoc = db.parse(file);

            SAXReader saxreader = new SAXReader();
            Document saxdoc = saxreader.read(file);
            DOMReader domreader = new DOMReader();
            Document dom4jdomdoc = domreader.read(domdoc);

            String[] xpath = {"//nsa:elema", "//nsa:elemA", "//nsb:elemb",
"//*[name()='elema']"};

            for (int i = 0; i < xpath.length; i++) {
                System.out.println();
                System.out.println("Trying " + xpath[i]);

                elem = (Element) dom4jdomdoc.selectSingleNode(xpath[i]);
                System.out.println("dom ===> " + elem);
                System.out.println("         XPath = " + ((elem != null) ?
elem.getPath() : "no xpath"));

                elem = (Element) saxdoc.selectSingleNode(xpath[i]);
                System.out.println("sax ===> " + elem);
                System.out.println("         XPath = " + ((elem != null) ?
elem.getPath() : "no xpath"));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Here's the output:

Trying //nsa:elema
dom ===> null
         XPath = no xpath
sax ===> org.dom4j.tree.DefaultElement@13d93f4 [Element: <elema uri:
http://www.aaa.com attributes: []/>]
         XPath = /*[name()='xmltest']/*[name()='elema']

Trying //nsa:elemA
dom ===> null
         XPath = no xpath
sax ===> org.dom4j.tree.DefaultElement@1749757 [Element: <nsa:elemA uri:
http://www.aaa.com attributes: []/>]
         XPath = /*[name()='xmltest']/nsa:elemA

Trying //nsb:elemb
dom ===> org.dom4j.tree.DefaultElement@eb017e [Element: <nsb:elemb uri:
http://www.bbb.com attributes: []/>]
         XPath = /*[name()='xmltest']/nsb:elemb
sax ===> org.dom4j.tree.DefaultElement@15b9e68 [Element: <nsb:elemb uri:
http://www.bbb.com attributes: []/>]
         XPath = /*[name()='xmltest']/nsb:elemb

Trying //*[name()='elema']
dom ===> org.dom4j.tree.DefaultElement@c53dce [Element: <elema uri:
http://www.aaa.com attributes: []/>]
         XPath = /*[name()='xmltest']/*[name()='elema']
sax ===> org.dom4j.tree.DefaultElement@13d93f4 [Element: <elema uri:
http://www.aaa.com attributes: []/>]
         XPath = /*[name()='xmltest']/*[name()='elema']




-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to