首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过属性值获取XML字符串

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

Stack Overflow用户
提问于 2020-10-01 09:24:52
回答 1查看 40关注 0票数 1

我正在尝试解析一个XML文件,通过步骤id获得一个“流程图”步骤,这是步骤的一个子元素:

代码语言:javascript
复制
<?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>

到目前为止,我拥有的Java代码将打印所有步骤和流程图代码,以及流程图描述,但是如何通过整数值请求特定步骤?

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-10-02 19:14:57

为了达到这个目的,使用XPath API会更加方便:

代码语言:javascript
复制
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);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64147874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档