gpt4 book ai didi

java - 使用 Java 解析 ALM XML 数据

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:35 24 4
gpt4 key购买 nike

有关解析以下内容的任何想法 - 我陷入了按标记名解析的困境,因为它们都是相同的。我只想获取每个值。

<Entities TotalResults="3">
<Entity Type="defect">
<Fields>
<Field Name="id">
<Value>2</Value>
</Field>
<Field Name="project">
<Value>P10</Value>
</Field>
<Field Name="name">
<Value>MMW - Issue with Referral check upon deployment of pre-assembly test</Value>
</Field>
</Fields>
<RelatedEntities/>
</Entity>
<Entity Type="defect">
<Fields>
<Field Name="id">
<Value>777</Value>
</Field>
<Field Name="project">
<Value>P10</Value>
</Field>
<Field Name="name">
<Value>R6 throwing CORBA transaction error on product set-up screen on every bundle selection</Value>
</Field>
</Fields>
<RelatedEntities/>
</Entity>

我正在尝试的代码如下 - 文件 fXmlFile = new File("D:/Java/file.xml");

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);

doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("Entity");
System.out.println("----------------------------");

for (int temp = 0; temp < nList.getLength(); temp++) {

Node nNode = nList.item(temp);

System.out.println("\nCurrent Element :" + nNode.getNodeName());

if (nNode.getNodeType() == Node.ELEMENT_NODE) {

Element eElement = (Element) nNode;
System.out.println("Name : " + eElement.getAttribute("Field"));

最佳答案

Element eElement = (Element) nNode;
System.out.println("Defect ID : " + Element.getElementsByTagName("Value").item(0).getTextContent());
System.out.println("Project : " + eElement.getElementsByTagName("Value").item(1).getTextContent());
System.out.println("Description : " + eElement.getElementsByTagName("Value").item(2).getTextContent());

关于java - 使用 Java 解析 ALM XML 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com