Java 如何通过属性值获取XML字符串

Java 如何通过属性值获取XML字符串,java,xml,parsing,Java,Xml,Parsing,我试图解析一个XML文件,以获得一个“流程图”,步骤id是步骤的子元素: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <CATALOG> <FLOWCHART> <PRIMARYCODE>FC1</PRIMARYCODE> <NAME>Flowchart 1</NAME&g

我试图解析一个XML文件,以获得一个“流程图”,步骤id是步骤的子元素:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CATALOG>
  <FLOWCHART>
    <PRIMARYCODE>FC1</PRIMARYCODE>
    <NAME>Flowchart 1</NAME>
    <STEPS>
      <STEP id="1">was powered on.</STEP>
      <STEP id="2">was not connected with a connection plate.</STEP>
    </STEPS>
  </FLOWCHART>
  <FLOWCHART>
    <PRIMARYCODE>FC2</PRIMARYCODE>
    <NAME>Flowchart2</NAME>
    <STEPS>
      <STEP id="1">was not powered on.</STEP>
      <STEP id="2">was connected with a connection plate.</STEP>
    </STEPS>
  </FLOWCHART>
</CATALOG>

FC1
流程图1
是通电的。
未与连接板连接。
FC2
流程图2
没有通电。
用连接板连接。
到目前为止,我所拥有的Java代码将打印所有步骤和流程图代码,以及流程图描述,但如何通过整数值请求特定步骤

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
public class Flowchart
{
    public static void main(String argv[])
    {
        try
        {
//creating a constructor of file class and parsing an XML file
            File file = new File("src/flowchart.xml");
//an instance of factory that gives a document builder
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//an instance of builder to parse the specified xml file
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
            NodeList nodeList = doc.getElementsByTagName("FLOWCHART");
// nodeList is not iterable, so we are using for loop
            for (int itr = 0; itr < nodeList.getLength(); itr++)
            {
                Node node = nodeList.item(itr);
                System.out.println("\nNode Name: " + node.getNodeName());
                if (node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element eElement = (Element) node;
                    System.out.println("Flowchart ID: "+ eElement.getElementsByTagName("PRIMARYCODE").item(0).getTextContent());
                    for (int i = 0; i < (eElement.getElementsByTagName("STEPS").getLength() + 1) ; i++)
                    {
                        System.out.println("Steps: "+ eElement.getElementsByTagName("STEP").item(i).getTextContent());
                    }

                }

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
import javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.DocumentBuilder;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.w3c.dom.Node;
导入org.w3c.dom.Element;
导入java.io.File;
公共类流程图
{
公共静态void main(字符串argv[])
{
尝试
{
//创建file类的构造函数并解析XML文件
File File=新文件(“src/flopport.xml”);
//提供文档生成器的factory实例
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
//用于分析指定xml文件的生成器实例
DocumentBuilder db=dbf.newDocumentBuilder();
文档doc=db.parse(文件);
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList NodeList=doc.getElementsByTagName(“流程图”);
//节点列表不可编辑,所以我们使用for循环
对于(int-itr=0;itr
为此,更方便的方法是使用XPath API:

import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public static String getStep(Document doc, String flowchartName, int stepId) throws XPathExpressionException {
        XPathFactory xpf = XPathFactory.newInstance();
        XPathExpression xpath = xpf.newXPath().compile("/CATALOG/FLOWCHART[NAME='" 
                + flowchartName 
                + "']/STEPS/STEP[@id='" 
                + stepId 
                + "']");
        return xpath.evaluate(doc);
    }