英文:
How to get an XML string by attribute value
问题
我正在尝试解析一个 XML 文件,以获取“流程图”中步骤的按步骤 ID 排列的内容,该 ID 是 steps 的子元素:
<?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 代码将打印出所有步骤、流程图代码和流程图描述,但是如何通过整数值请求特定的步骤?
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
{
// 创建文件类的构造函数并解析 XML 文件
File file = new File("src/flowchart.xml");
// 获取文档构建器的实例的工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 创建一个解析指定 XML 文件的构建器的实例
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 不能迭代,因此我们使用 for 循环
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();
}
}
}
英文:
I am trying to parse an XML file to get a "flowchart" step by the step id which is a child element of steps:
<?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>
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?
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();
}
}
}
答案1
得分: 1
为了实现这一目的,使用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);
}
英文:
For this purpose much more convenient would be usage of 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论