Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Xerces-J does not get the namespace correctly

Question posted by: Philipp (Guest) on June 27th, 2008 07:07 PM
Hello,
I am not able to read the namespace for my elements correctly. Could
somebody point me to my error. Thank you. Philipp

Compilable example:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLTest {
public static void main(String[] args) {
String xmlString =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
"<e:test xmlns:e=\"http://www.example.com/e\"/>";

InputStream dataStream = new
ByteArrayInputStream(xmlString.getBytes());
Document document = null;
try {
document =
DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(dataStream);
} catch (Exception e1) {
e1.printStackTrace();
}

Element root = document.getDocumentElement();
System.out.println("prefix: " + root.getPrefix());
System.out.println("name: " + root.getNodeName() );
System.out.println("localname: " + root.getLocalName() );
System.out.println("ns_uri: " + root.getNamespaceURI());

// PRINTS:
// prefix: null
// name: e:test
// localname: null
// ns_uri: null

// EXPECTED:
// prefix: e
// name: e:test
// localname: test
// ns_uri: http://www.example.com/e
}
}
Joseph J. Kesselman's Avatar
Joseph J. Kesselman
Guest
n/a Posts
June 27th, 2008
07:07 PM
#2

Re: Xerces-J does not get the namespace correctly
I think you still have to explicitly set Xerces into namespace-aware mode.

http://xerces.apache.org/xerces2-j/features.html

Philipp's Avatar
Philipp
Guest
n/a Posts
June 27th, 2008
07:07 PM
#3

Re: Xerces-J does not get the namespace correctly
Joseph J. Kesselman wrote:
Quote:
I think you still have to explicitly set Xerces into namespace-aware mode.
>
http://xerces.apache.org/xerces2-j/features.html


Thanks. That solved my problem!

 
Not the answer you were looking for? Post your question . . .
189,846 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors