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

huangapple go评论108阅读模式
英文:

How to get an XML string by attribute value

问题

我正在尝试解析一个 XML 文件,以获取“流程图”中步骤的按步骤 ID 排列的内容,该 ID 是 steps 的子元素:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <CATALOG>
  3. <FLOWCHART>
  4. <PRIMARYCODE>FC1</PRIMARYCODE>
  5. <NAME>Flowchart 1</NAME>
  6. <STEPS>
  7. <STEP id="1">was powered on.</STEP>
  8. <STEP id="2">was not connected with a connection plate.</STEP>
  9. </STEPS>
  10. </FLOWCHART>
  11. <FLOWCHART>
  12. <PRIMARYCODE>FC2</PRIMARYCODE>
  13. <NAME>Flowchart2</NAME>
  14. <STEPS>
  15. <STEP id="1">was not powered on.</STEP>
  16. <STEP id="2">was connected with a connection plate.</STEP>
  17. </STEPS>
  18. </FLOWCHART>
  19. </CATALOG>

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

  1. import javax.xml.parsers.DocumentBuilderFactory;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import org.w3c.dom.Document;
  4. import org.w3c.dom.NodeList;
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.Element;
  7. import java.io.File;
  8. public class Flowchart
  9. {
  10. public static void main(String argv[])
  11. {
  12. try
  13. {
  14. // 创建文件类的构造函数并解析 XML 文件
  15. File file = new File("src/flowchart.xml");
  16. // 获取文档构建器的实例的工厂
  17. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  18. // 创建一个解析指定 XML 文件的构建器的实例
  19. DocumentBuilder db = dbf.newDocumentBuilder();
  20. Document doc = db.parse(file);
  21. doc.getDocumentElement().normalize();
  22. System.out.println("Root element: " + doc.getDocumentElement().getNodeName());
  23. NodeList nodeList = doc.getElementsByTagName("FLOWCHART");
  24. // nodeList 不能迭代,因此我们使用 for 循环
  25. for (int itr = 0; itr < nodeList.getLength(); itr++)
  26. {
  27. Node node = nodeList.item(itr);
  28. System.out.println("\nNode Name: " + node.getNodeName());
  29. if (node.getNodeType() == Node.ELEMENT_NODE)
  30. {
  31. Element eElement = (Element) node;
  32. System.out.println("Flowchart ID: "+ eElement.getElementsByTagName("PRIMARYCODE").item(0).getTextContent());
  33. for (int i = 0; i < (eElement.getElementsByTagName("STEPS").getLength() + 1) ; i++)
  34. {
  35. System.out.println("Steps: "+ eElement.getElementsByTagName("STEP").item(i).getTextContent());
  36. }
  37. }
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. e.printStackTrace();
  43. }
  44. }
  45. }
英文:

I am trying to parse an XML file to get a "flowchart" step by the step id which is a child element of steps:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;
  2. &lt;CATALOG&gt;
  3. &lt;FLOWCHART&gt;
  4. &lt;PRIMARYCODE&gt;FC1&lt;/PRIMARYCODE&gt;
  5. &lt;NAME&gt;Flowchart 1&lt;/NAME&gt;
  6. &lt;STEPS&gt;
  7. &lt;STEP id=&quot;1&quot;&gt;was powered on.&lt;/STEP&gt;
  8. &lt;STEP id=&quot;2&quot;&gt;was not connected with a connection plate.&lt;/STEP&gt;
  9. &lt;/STEPS&gt;
  10. &lt;/FLOWCHART&gt;
  11. &lt;FLOWCHART&gt;
  12. &lt;PRIMARYCODE&gt;FC2&lt;/PRIMARYCODE&gt;
  13. &lt;NAME&gt;Flowchart2&lt;/NAME&gt;
  14. &lt;STEPS&gt;
  15. &lt;STEP id=&quot;1&quot;&gt;was not powered on.&lt;/STEP&gt;
  16. &lt;STEP id=&quot;2&quot;&gt;was connected with a connection plate.&lt;/STEP&gt;
  17. &lt;/STEPS&gt;
  18. &lt;/FLOWCHART&gt;
  19. &lt;/CATALOG&gt;

The Java Code that I have thus far will print all the steps and the flowchart code, and flowchart description but how to I request a specific step by an integer value?

  1. import javax.xml.parsers.DocumentBuilderFactory;
  2. import javax.xml.parsers.DocumentBuilder;
  3. import org.w3c.dom.Document;
  4. import org.w3c.dom.NodeList;
  5. import org.w3c.dom.Node;
  6. import org.w3c.dom.Element;
  7. import java.io.File;
  8. public class Flowchart
  9. {
  10. public static void main(String argv[])
  11. {
  12. try
  13. {
  14. //creating a constructor of file class and parsing an XML file
  15. File file = new File(&quot;src/flowchart.xml&quot;);
  16. //an instance of factory that gives a document builder
  17. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  18. //an instance of builder to parse the specified xml file
  19. DocumentBuilder db = dbf.newDocumentBuilder();
  20. Document doc = db.parse(file);
  21. doc.getDocumentElement().normalize();
  22. System.out.println(&quot;Root element: &quot; + doc.getDocumentElement().getNodeName());
  23. NodeList nodeList = doc.getElementsByTagName(&quot;FLOWCHART&quot;);
  24. // nodeList is not iterable, so we are using for loop
  25. for (int itr = 0; itr &lt; nodeList.getLength(); itr++)
  26. {
  27. Node node = nodeList.item(itr);
  28. System.out.println(&quot;\nNode Name: &quot; + node.getNodeName());
  29. if (node.getNodeType() == Node.ELEMENT_NODE)
  30. {
  31. Element eElement = (Element) node;
  32. System.out.println(&quot;Flowchart ID: &quot;+ eElement.getElementsByTagName(&quot;PRIMARYCODE&quot;).item(0).getTextContent());
  33. for (int i = 0; i &lt; (eElement.getElementsByTagName(&quot;STEPS&quot;).getLength() + 1) ; i++)
  34. {
  35. System.out.println(&quot;Steps: &quot;+ eElement.getElementsByTagName(&quot;STEP&quot;).item(i).getTextContent());
  36. }
  37. }
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. e.printStackTrace();
  43. }
  44. }
  45. }

答案1

得分: 1

为了实现这一目的,使用XPath API会更加方便:

  1. import javax.xml.xpath.XPathExpression;
  2. import javax.xml.xpath.XPathExpressionException;
  3. import javax.xml.xpath.XPathFactory;
  4. public static String getStep(Document doc, String flowchartName, int stepId) throws XPathExpressionException {
  5. XPathFactory xpf = XPathFactory.newInstance();
  6. XPathExpression xpath = xpf.newXPath().compile("/CATALOG/FLOWCHART[NAME='"
  7. + flowchartName
  8. + "']/STEPS/STEP[@id='"
  9. + stepId
  10. + "']");
  11. return xpath.evaluate(doc);
  12. }
英文:

For this purpose much more convenient would be usage of XPath API:

  1. import javax.xml.xpath.XPathExpression;
  2. import javax.xml.xpath.XPathExpressionException;
  3. import javax.xml.xpath.XPathFactory;
  4. public static String getStep(Document doc, String flowchartName, int stepId) throws XPathExpressionException {
  5. XPathFactory xpf = XPathFactory.newInstance();
  6. XPathExpression xpath = xpf.newXPath().compile(&quot;/CATALOG/FLOWCHART[NAME=&#39;&quot;
  7. + flowchartName
  8. + &quot;&#39;]/STEPS/STEP[@id=&#39;&quot;
  9. + stepId
  10. + &quot;&#39;]&quot;);
  11. return xpath.evaluate(doc);
  12. }

huangapple
  • 本文由 发表于 2020年10月1日 09:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64147874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定