如何从<;dc>;java中的标签?

如何从<;dc>;java中的标签?,java,epub,Java,Epub,我目前正试图从Java中的epub中提取标记元素。然而,我尝试使用 doc.getDocumentElement().getElementsByTagName("dc:title")); 它只显示了2个元素:com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl。我想知道如何提取 这是我的代码: File fXmlFile = new File("file directory"); DocumentBuilderFactory dbFa

我目前正试图从Java中的epub中提取标记元素
。然而,我尝试使用

doc.getDocumentElement().getElementsByTagName("dc:title")); 
它只显示了
2个元素:com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl
。我想知道如何提取

这是我的代码:

File fXmlFile = new File("file directory");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();

System.out.println("1st element :" +  doc.getElementsByTagName("dc");
System.out.println("2nd element :" + doc.getDocumentElement().getElementsByTagName("dc:title"));
系统输出:

1st element : com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@4f53e9be
2nd element :com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl@e16e1a2
添加了样本数据

<dc:title>
  <![CDATA[someData]]>
</dc:title>
<dc:creator>
  <![CDATA[someData]>
</dc:creator>
<dc:language>someData</dc:language>

一些数据

方法
getElementsByTagName(String)
返回匹配元素的列表(注意复数's')。然后需要指定要使用的元素(例如通过使用
.item(index)
访问节点实例)。因此,您可以在该
节点上使用
getNodeValue()

已编辑:由于CDATA元素,请使用
Node.getTextContent()


我建议使用xpath获得所需的输出。 另外,请参考以下链接以获取示例。 例如:

XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//dc:title/text()";
NodeList nodes = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
System.out.println(nodes.item(0).getNodeValue());

我确实测试了你的方法,但仍然不起作用。对于索引(0)=NULL,索引(1)=java.lang.NullPointerException。我添加了一些我想在帖子中显示的示例数据。您的选择器看起来有问题。“dc”是名称空间前缀,而不是标记名。它是否可以使用:
doc.getDocumentElement().getElementsByTagnames(“*”,“title”)?另外,您的示例看起来格式不好-您应该添加一个公共根元素。我尝试使用您的建议。仍然返回相同的结果,如
2nd元素:com.sun.org.apache.xerces.internal.dom。DeepNodeListImpl@7e0d8db1
该示例只是部分代码。它有根元素,根元素类似于这样的
,但是您现在可以在返回节点列表对象上调用
.item(0).getNodeValue()
?@Drew-好的,我忘记了CDATA。我编辑了我的答案,也许这有助于
dc:
部分是名称空间前缀。您应该使用名称空间感知来解析XML文档。例子:
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "//dc:title/text()";
NodeList nodes = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
System.out.println(nodes.item(0).getNodeValue());