使用Java在一个标记中读取具有多个值的XML文件

时间:2018-05-24 06:37:12

标签: java xml dom xml-parsing

我有这样的XML文件

<Customers>
  <Customer Name="Test_91" Code="91" Purpose="Supplier" />
  <Customer Name="Test_92" Code="92" Purpose="Receiver" />
</Customers>

我需要用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 XMLReaderDOM {

    public static void main(String argv[]) {
        try {
            File fXmlFile = new File("Customers.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("Customers");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;


                    System.out.println("Customer id : "
                            + ((Element) eElement.getElementsByTagName("Customer").item(0)).getAttribute("Name"));
                    System.out.println("IataCode : "
                            + ((Element) eElement.getElementsByTagName("Customer").item(0)).getAttribute("Code"));
                    System.out.println("IcaoCode : "
                            + ((Element) eElement.getElementsByTagName("Customer").item(0)).getAttribute("Purpose"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

但我没有得到第二记录的价值。这是客户“Test_92”的详细信息。

有人可以帮助我阅读N个客户的价值吗?

提前致谢!

4 个答案:

答案 0 :(得分:2)

你正在绕过破旧的东西。

您获得了名为“customers”的元素列表(您只有其中一个)然后循环遍历它。

然后在循环中找到“customer”列表,取第一个条目然后提取属性(三次)。

您需要做的是,获取“客户”列表的第一个条目,然后获取“客户”列表并循环。

答案 1 :(得分:0)

尝试使用xPath。

您可以轻松地从节点跳转到节点。

XML:

<Customers>
   <Customer name='f1'/>
   <Customer name='f1'/>
   <Customer name='f1'/>
</Customers>

爪哇:

Document xmlDocument = builder.parse(new FileInputStream("Customers.xml"));

XPath xPath =  XPathFactory.newInstance().newXPath();

NodeList nodeList = (NodeList) xPath.compile("/Customers/Customer").evaluate(xmlDocument,  XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); i++)
{
    System.out.println(nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
}

Outpot:

f1
f2
f3

答案 2 :(得分:0)

您将获得Customers标记列表,该列表仅在您的文档中。

试试这个,

 NodeList nList = doc.getElementsByTagName("Customer");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;


                    System.out.println("Customer id : "
                            + ( eElement.getAttribute("Name")));
                    System.out.println("IataCode : "
                            + ( eElement.getAttribute("Code")));
                    System.out.println("IcaoCode : "
                            + ( eElement.getAttribute("Purpose")));

答案 3 :(得分:0)

SimpleXml可以做到:

final String data = ...
final SimpleXml simple = new SimpleXml();
final Element customers = simple.fromXml(data);
for (final Element customer : customers.children) {
    System.out.println(customer.attributes.get("Name"));
}

将输出:

Test_91
Test_92

从Maven Central:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>
相关问题