Java XML在特定节点上提取数据

时间:2015-07-21 09:08:11

标签: java xml xmlnodelist

我试图通过搜索特定节点来提取所有数据。我下面的代码只能打印出所有节点,但是如果我只想在pantone 101上提取信息并打印出所有其他节点,例如特定pantone 101节点内的颜色,该怎么办。这是我创建的用于打印XML数据的代码,如何编辑它以仅打印特定节点。谢谢!

<inventory>
    <Product pantone="100" blue="7.4" red="35" green="24"> </Product>
    <Product pantone="101" blue="5.4" red="3" rubine="35" purple="24"> </Product>
    <Product pantone="102" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="103" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="104" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="105" orange="5.4" purple="35" white="24"> </Product>
    <Product pantone="106" black="5.4" rubine="35" white="24" purple="35" orange="5.4"> </Product>
</inventory>

//

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ReadXML {

    public static void main(String args[]) throws Exception {
        DocumentBuilderFactory buildFactory = DocumentBuilderFactory.newInstance();
        try {
        DocumentBuilder dBuilder = buildFactory.newDocumentBuilder();
        Document document = dBuilder.parse(ReadXML.class.getResourceAsStream("data.xml"));
        document.normalize();

        //get main node
        NodeList rootNodes = document.getElementsByTagName("inventory");
        Node rootNode = rootNodes.item(0);
        Element rootElement = (Element) rootNode;

        //print all with specific tag
        NodeList inventoryList = rootElement.getElementsByTagName("Product");
        for(int i = 0; i < inventoryList.getLength(); i++){
            Node pantone = inventoryList.item(i);
            Element pantoneElement = (Element) pantone;

            //remove blank elements

            System.out.println("Pantone: " + pantoneElement.getAttribute("pantone")); // print attribute
            System.out.println("Blue: " + pantoneElement.getAttribute("blue"));
            System.out.println("Red: " + pantoneElement.getAttribute("red"));
            System.out.println("Orange: " + pantoneElement.getAttribute("orange"));
            System.out.println("White: " + pantoneElement.getAttribute("white"));
            System.out.println("Purple: " + pantoneElement.getAttribute("purple"));
            System.out.println("Green: " + pantoneElement.getAttribute("green"));
            System.out.println("Black: " + pantoneElement.getAttribute("black"));
            System.out.println("Rubine: " + pantoneElement.getAttribute("rubine"));
        }

        } catch (ParserConfigurationException | SAXException | IOException e) {
        }

    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用XPath使用更高级的条件从XML文档中获取数据。例如,以下是XPath表达式,它将<Product>元素的pantone属性等于101

/inventory/Product[@pantone=101]

或者,如果您计划在/inventory元素上调用XPath作为上下文,则不要在XPath中提及<inventory>

Product[@pantone=101]

我不熟悉Java,但这篇文章应该给你一个很好的提示:How to read XML using XPath in Java