除了使用Xpath之外还有什么方法吗?

时间:2015-09-11 21:54:20

标签: java xml dom xpath

你好我们正在编写这个程序:

src

我想打印关于"科幻小说的所有数据"书籍..我知道我应该使用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 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());
         } 
      }
   }
}
&#13;
&#13;
&#13;

假设我有这张桌子,我只想用他们所有的信息打印科幻小说......

1 个答案:

答案 0 :(得分:2)

  

我想打印关于&#34;科幻小说的所有数据&#34;书籍..我知道我应该使用Xpath,但它被卡住了,

我认为你的意思是你想要genre == "Science Fiction"的所有书籍,对吗?在这种情况下,XPath实际上比你在Java中尝试的任何东西都要简单得多(你不会显示根音,所以我将从&#39; //&#39;开始,选择at任何深度):

//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

如果您可以在Java中使用Saxon,那么上面的内容将成为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)
)