Java 除了使用Xpath,还有其他方法吗?
大家好,我正在写这个程序:Java 除了使用Xpath,还有其他方法吗?,java,xml,dom,xpath,Java,Xml,Dom,Xpath,大家好,我正在写这个程序: import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class DOMbooks { public sta
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DOMbooks {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
File file = new File("books-fixed.xml");
Document doc = docBuilder.parse(file);
NodeList list = doc.getElementsByTagName("*");
int bookCounter = 1;
for (int i = 1; i < list.getLength(); i++) {
Element element = (Element)list.item(i);
String nodeName = element.getNodeName();
if (nodeName.equals("book")) {
bookCounter++;
System.out.println("BOOK " + bookCounter);
String isbn = element.getAttribute("sequence");
System.out.println("\tsequence:\t" + isbn);
}
else if (nodeName.equals("author")) {
System.out.println("\tAuthor:\t" + element.getChildNodes().item(0).getNodeValue());
}
else if (nodeName.equals("title")) {
System.out.println("\tTitle:\t" + element.getChildNodes().item(0).getNodeValue());
}
else if (nodeName.equals("publishYear")) {
System.out.println("\tpublishYear:\t" + element.getChildNodes().item(0).getNodeValue());
}
else if (nodeName.equals("genre")) {
System.out.println("\tgenre:\t" + element.getChildNodes().item(0).getNodeValue());
}
}
}
}
导入java.io.File;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.NodeList;
公共类图书{
公共静态void main(字符串[]args)引发异常{
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=factory.newDocumentBuilder();
File File=新文件(“books fixed.xml”);
Document doc=docBuilder.parse(文件);
节点列表=doc.getElementsByTagName(“*”);
int bookCounter=1;
对于(int i=1;i
我想把所有关于“科幻小说”的资料都打印出来。。我知道我应该使用Xpath,但它被卡住了,错误太多。。。有什么建议吗?
假设我有这个表,我只想选择包含所有信息的科幻小说
余震
罗伯特·B·里奇
2010
经济
-
时光机器
H.G.威尔斯
1895
科幻小说
我想把所有关于“科幻小说”的资料都打印出来。。我知道我应该使用Xpath,但它被卡住了
我想你的意思是你想要所有的类型==“科幻小说”
,对吗?在这种情况下,XPath实际上比您在Java中尝试的要简单得多(您没有显示根注释,因此我将以“/”,它在任何深度进行选择):
//书籍[体裁='科幻小说'
简化事情的XSLT方法
现在,再看一下您的代码,看起来您想要打印每个元素,包括元素的名称。这在XSLT中更为简单:
<!-- every XSLT 1.0 must start like this -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- you want text -->
<xsl:output method="text" />
<!-- match any science fiction book (your primary goal) -->
<xsl:template match="book[genre = 'Science Fiction']">
<xsl:text>BOOK </xsl:text>
<xsl:value-of select="position()" />
<!-- send the children and attribute to be processed by templates -->
<xsl:apply-templates select="@sequence | *" />
</xsl:template>
<!-- "catch" any elements or attributes under <book> -->
<xsl:template match="book/* | book/@*">
<!-- a newline and a tab per line-->
<xsl:text>
	</xsl:text>
<!-- and the name of the element or attribute -->
<xsl:value-of select="local-name()" />
<!-- another tab, plus contents of the element or attribute -->
<xsl:text>	</xsl:text>
<xsl:value-of select="." />
</xsl:template>
<!-- make sure that other values are ignored, but process children -->
<xsl:template match="node()">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
XPath2.0
如果可以在Java中使用,上述内容将成为XPath 2.0的一行代码,您甚至不需要XSLT:
for$book in//book[体裁='科幻小说']
返回(
“书”,
count(//book[genre='scientific'][。为什么XPath会有太多错误?它是查询XML的事实工具,已经有15年了,而且非常稳定。遇到错误(假设您是指处理器中的bug)时使用的处理器是什么是的,我删除了我编写Xpath块的部分,实际上这是垃圾。我导入了许多不必要的包,编写了许多不必要的代码行,我对它完全陌生。我已经尝试了4个小时,但workLet似乎没有后退一步。为什么不给我们看一个(小的,但相关的)输入XML的一部分,同样用于预期的输出XML。可能XPath不好,您需要XSLT。许多人使用Java和XML技术没有问题,但使用像您现在这样更难的技术会使情况更糟…(imo)我编辑了它,您现在可以开始了…非常感谢Abel:D
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
public class TestMain {
public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("books.xsl"));
Transformer transformer = factory.newTransformer(xslt);
Source text = new StreamSource(new File("books-fixed.xml"));
transformer.transform(text, new StreamResult(new File("output.txt")));
}
}