为什么java中的getElementsByTagNameNS为空? [英] Why is the getElementsByTagNameNS empty in java?
问题描述
我想从 tag2 中获取值,我得到了一个 xml:
I want to get the value out tag2 and I got this a xml:
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ns1:schema xmlns:ns1='http://example.com'>" +
"<ns1:tag1>" +
" <ns1:tag2>value</ns1:tag2>" +
"</ns1:tag1>" +
"</ns1:schema>";
然后解析成文档,想通过tagnameNS获取元素.但是当我运行这个时,节点列表是空的,为什么?
Then parse it to a document and want to get the elements by tagnameNS. But when I run this the nodelist is empty why?
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
NodeList nl = doc.getElementsByTagNameNS("http://example.com", "tag2");
String a = nl.item(0).getNodeValue();
仍然不能使用 URI.
Still doesnt work with the URI.
推荐答案
getElementsByTagNameNS
正在返回结果.问题是您当前调用了错误的方法来从结果元素中获取文本内容.您需要调用 getTextContext()
而不是 getNodeValue()
getElementsByTagNameNS
is returing a result. The issue is that you're currently calling the wrong method to get the text content off the result elements. You need to call getTextContext()
and not getNodeValue()
String a = nl.item(0).getTextContent();
DomDemo
下面是一个完整的代码示例.
Below is a complete code example.
package forum13166195;
import java.io.StringReader;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
public class DomDemo {
public static void main(String[] args) throws Exception{
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<ns1:schema xmlns:ns1='http://example.com'>"
+ "<ns1:tag1>"
+ "<ns1:tag2>value</ns1:tag2>"
+ "</ns1:tag1>"
+ "</ns1:schema>";
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(xml)));
NodeList nl = doc.getElementsByTagNameNS("http://example.com", "tag2");
String a = nl.item(0).getTextContent();
System.out.println(a);
}
}
输出
value
<小时>
替代方法
您还可以使用 javax.xml.xpath
API(包含在 Java SE 5 及更高版本中)从 XML 文档查询值.这些 API 提供了比 getElementsByTagNameNS
更多的控制.
You can also use the javax.xml.xpath
APIs (included in Java SE 5 and above) to query a value from an XML document. These APIs offer a lot more control than getElementsByTagNameNS
.
XPathDemo
package forum13166195;
import java.io.StringReader;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.*;
import org.xml.sax.InputSource;
public class XPathDemo {
public static void main(String[] args) throws Exception{
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ "<ns1:schema xmlns:ns1='http://example.com'>"
+ "<ns1:tag1>"
+ "<ns1:tag2>value</ns1:tag2>"
+ "</ns1:tag1>"
+ "</ns1:schema>";
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String arg0) {
if("a".equals(arg0)) {
return "http://example.com";
}
return null;
}
public String getPrefix(String arg0) {
return null;
}
public Iterator getPrefixes(String arg0) {
return null;
}
});
InputSource inputSource = new InputSource(new StringReader(xml));
String result = (String) xpath.evaluate("/a:schema/a:tag1/a:tag2", inputSource, XPathConstants.STRING);
System.out.println(result);
}
}
输出
value
这篇关于为什么java中的getElementsByTagNameNS为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!