有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试图通过搜索特定节点来提取所有数据。我下面的代码只能打印出所有节点,但如果我只想获取有关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) 个答案

  1. # 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