gpt4 book ai didi

java - 除了为此使用 Xpath 之外还有其他方法吗?

转载 作者:行者123 更新时间:2023-11-30 08:47:15 24 4
gpt4 key购买 nike

大家好,我正在写这个程序:

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());
}
}
}
}

我想打印有关“科幻小说”书籍的所有数据。我知道我应该使用 Xpath,但它卡住了,错误太多……有什么建议吗?假设我有这张表,我只想选择包含所有信息的科幻小说

 <book sequence="5">
<title>Aftershock</title>
<auther>Robert B. Reich</auther>
<publishYear>2010</publishYear>
<genre>Economics</genre>
</book>
- <book sequence="6">
<title>The Time Machine</title>
<auther>H.G. Wells</auther>
<publishYear>1895</publishYear>
<genre>Science Fiction</genre>

假设我有这张表,我只想打印科幻小说及其所有信息...

最佳答案

i want to print all the data about the "Science Fiction" books.. i know i should use Xpath but it's stuck,

我假设您的意思是您想要所有 genre == "Science Fiction" 的书,对吗?在这种情况下,XPath 确实比您在 Java 中尝试的任何方法都简单得多(您不显示根注释,所以我将从“//”开始,它可以在任何深度进行选择):

//book[genre = 'Science Fiction']

简化事情的 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>&#xA;&#9;</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>&#9;</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>

您可以使用这段代码,它比您的原始代码更短(如果您忽略注释和空格)并且(可以说,一旦您掌握了它)更易读。要使用它:

  • 将其存储为 books.xsl
  • 然后,只需使用此 ( copied and changed from here):

    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")));
    }
    }

XPath 2.0

如果可以使用Saxon在 Java 中,以上内容成为 XPath 2.0 的单行代码,您甚至不需要 XSLT:

for $book in //book[genre = 'Science Fiction']
return (
'BOOK',
count(//book[genre = 'Science Fiction'][. << $book]) + 1,
for $tag in $book/(@sequence | *)
return $tag/local-name(), ':', string($tag)
)

关于java - 除了为此使用 Xpath 之外还有其他方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32532678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com