Java中所有子元素读取XML

时间:2011-12-20 16:16:32

标签: java xml xml-parsing

我想同时读取所有链接和标题到数组列表?

<rootNode>
    <image>
        <link>http://imagelink/</link>
        <title>This is the title</title>
    </image>
    <movie>
        <link>http://movielink/</link>
        <title>This is the title</title>
    </movie>
</rootNode>

下面的代码工作正常但是,如果有不同的子节点,它可能会使代码太大。

我不想要多个,我希望看起来干净的东西!

    ArrayList XTimeStamp = new ArrayList();
    Node libraryTagNode, libraryDataNode;
    Element libraryTagElement, libraryDataElement;
    NodeList libraryTagList,libraryDataList;

    try {
        File file = new File("computer.txt");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);
        doc.getDocumentElement().normalize();

        NodeList imageLst = doc.getElementsByTagName("image");
        NodeList movieLst = doc.getElementsByTagName("movie");  

        for (int s = 0; s < imageLst.getLength(); s++) {
            libraryTagNode=bookLst.item(s);


            if (libraryTagNode.getNodeType() == Node.ELEMENT_NODE) {

                libraryTagElement = (Element) libraryTagNode;

                libraryTagList = libraryTagElement.getElementsByTagName("link");
                libraryDataElement = (Element) libraryTagList.item(0);

                libraryDataList = libraryDataElement.getChildNodes();
                String timeStampX=libraryDataList.item(0).getNodeValue();

                libraryTagList = libraryTagElement.getElementsByTagName("title");
                libraryDataElement = (Element) libraryTagList.item(0);

                libraryDataList = libraryDataElement.getChildNodes();
                String timeStampY=libraryDataList.item(0).getNodeValue();

                XTimeStamp.add(timeStampX+" "+timeStampY);

            }

        }
    for (int s = 0; s < movieLst.getLength(); s++) {
            libraryTagNode=bookLst.item(s);


            if (libraryTagNode.getNodeType() == Node.ELEMENT_NODE) {

                libraryTagElement = (Element) libraryTagNode;

                libraryTagList = libraryTagElement.getElementsByTagName("link");
                libraryDataElement = (Element) libraryTagList.item(0);

                libraryDataList = libraryDataElement.getChildNodes();
                String timeStampX=libraryDataList.item(0).getNodeValue();

                libraryTagList = libraryTagElement.getElementsByTagName("title");
                libraryDataElement = (Element) libraryTagList.item(0);

                libraryDataList = libraryDataElement.getChildNodes();
                String timeStampY=libraryDataList.item(0).getNodeValue();

                XTimeStamp.add(timeStampX+" "+timeStampY);

            }

        }

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

不是100%肯定您的要求,但如果您想获取NodeList中的所有链接节点以及不同节点列表中的所有标题节点,您可以尝试XPath使用“// link”和“/ / title“作为你的XPath表达式

答案 2 :(得分:0)

我猜你想要以某种方式将这两个循环合二为一,因为这实际上是一种繁琐的方式。

答案是使用XPath。我建议您搜索<title>包含parent元素的<link>个元素。