0

特定のノードを検索してすべてのデータを取得しようとしています。以下のコードはすべてのノードのみを出力できますが、たとえば 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) {
        }

    }
}
4

1 に答える 1

1

XPath を使用して、より高度な基準を使用して XML ドキュメントからデータを取得できます。たとえば、属性 equals<Product>を持つ要素を取得する XPath 式は次のとおりです。pantone101

/inventory/Product[@pantone=101]

または、要素の XPath をコンテキストとして/inventory呼び出す場合は、XPath で言及しないでください。<inventory>

Product[@pantone=101]

私は Java に詳しくありませんが、この記事は良いヒントになるはずです: Java で XPath を使用して XML を読み取る方法

于 2015-07-21T09:30:37.820 に答える