- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
为什么 getNamespaceURI() 总是返回 null? printNSInfo 方法有什么问题
public static void main(String[] args) {
Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(args[0]);
Element root = input.getDocumentElement();
printNSInfo(root);
}
private static void printNSInfo(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNamespaceURI() != null) {
System.out.println("Element Name:" + node.getNodeName());
System.out.println("Local Name:" + node.getLocalName());
System.out.println("Namespace Prefix:" + node.getPrefix());
System.out.println("Namespace Uri:" + node.getNamespaceURI());
System.out.println("---------------");
}
if (node.hasAttributes()) {
NamedNodeMap map = node.getAttributes();
int len = map.getLength();
for (int i = 0; i < len; i++) {
Node attr = map.item(i);
if (attr.getNamespaceURI() != null) {
printNSInfo(attr);
}
}
}
Node child = node.getFirstChild();
System.out.println(child);
while (child != null) {
printNSInfo(child);
child = child.getNextSibling();
}
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
System.out.println("Attribute Name:" + node.getNodeName());
System.out.println("Local Name:" + node.getLocalName());
System.out.println("Namespace Prefix:" + node.getPrefix());
System.out.println("Namespace Uri:" + node.getNamespaceURI());
System.out.println("---------------");
}
}
输入的 xml 文件是:
<a:NormalExample xmlns:a="http://sonormal.org/" xmlns:b="http://stillnormal.org/">
<a:AnElement b:anAttribute="text"> </a:AnElement>
</a:NormalExample>
当我在 eclipse 中调试时,node.getNamespaceURI()
总是返回 null
,我哪里错了?
最佳答案
来自 this ,你需要设置一个标志factory.setNamespaceAware(true)
,像这样:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document input = builder.parse(args[0]);
Element root = input.getDocumentElement();
printNSInfo(root);
输出是:
Element Name:a:NormalExample
Local Name:NormalExample
Namespace Prefix:a
Namespace Uri:http://sonormal.org/
---------------
...continue...
关于java - 为什么 getNamespaceURI() 总是返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27715183/
为什么 getNamespaceURI() 总是返回 null? printNSInfo 方法有什么问题 public static void main(String[] args) { Do
本文整理了Java中javolution.xml.stream.XMLStreamReader.getNamespaceURI()方法的一些代码示例,展示了XMLStreamReader.getNam
本文整理了Java中org.apache.woden.XMLElement.getNamespaceURI()方法的一些代码示例,展示了XMLElement.getNamespaceURI()的具体用
本文整理了Java中org.eclipse.persistence.oxm.XMLRoot.getNamespaceURI()方法的一些代码示例,展示了XMLRoot.getNamespaceURI(
本文整理了Java中org.eclipse.persistence.internal.oxm.XPathFragment.getNamespaceURI()方法的一些代码示例,展示了XPathFrag
本文整理了Java中org.deegree.commons.xml.stax.XMLStreamReaderWrapper.getNamespaceURI()方法的一些代码示例,展示了XMLStrea
本文整理了Java中com.google.gwt.uibinder.rebind.XMLElement.getNamespaceUri()方法的一些代码示例,展示了XMLElement.getName
我是一名优秀的程序员,十分优秀!