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.ByteArr ayInputStream;
import java.io.InputSt ream;
import javax.xml.parse rs.DocumentBuil derFactory;
import org.w3c.dom.Doc ument;
import org.w3c.dom.Ele ment;
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
ByteArrayInputS tream(xmlString .getBytes());
Document document = null;
try {
document =
DocumentBuilder Factory
.newInstance()
.newDocumentBui lder()
.parse(dataStre am);
} catch (Exception e1) {
e1.printStackTr ace();
}
Element root = document.getDoc umentElement();
System.out.prin tln("prefix: " + root.getPrefix( ));
System.out.prin tln("name: " + root.getNodeNam e() );
System.out.prin tln("localname: " + root.getLocalNa me() );
System.out.prin tln("ns_uri: " + root.getNamespa ceURI());
// 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
}
}