如何使用相同的父标签和子标签解析Java中的XML?

时间:2018-08-03 13:22:08

标签: java xml xml-parsing

考虑此xml:

<?xml version="1.0"?>
<company>
    <staff id="1001">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>  

我需要检索XML标记值。当没有相同名称的父节点和子节点时,以下代码可以完美地工作。

File fXmlFile = new File("xmls.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("staff");
    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("Staff id : "
                               + eElement.getAttribute("id"));
            System.out.println("First Name : "
                               + eElement.getElementsByTagName("firstname")
                                 .item(0).getTextContent());
            System.out.println("Last Name : "
                               + eElement.getElementsByTagName("lastname")
                                 .item(0).getTextContent());
            System.out.println("Nick Name : "
                               + eElement.getElementsByTagName("nickname")
                                 .item(0).getTextContent());
            System.out.println("Salary : "
                               + eElement.getElementsByTagName("salary")
                                 .item(0).getTextContent()); }}

当xml中有相同的父节点和子节点时,我需要在代码中提供多少机会,如下所示。

<?xml version="1.0"?>
    <company>
        <staff id="1001">
            <firstname>yong</firstname>
            <lastname>mook kim</lastname>
            <nickname>mkyong</nickname>
            <salary>100000</salary>
            <staff>10</staff>
        </staff>
        <staff id="2001">
            <firstname>low</firstname>
            <lastname>yin fong</lastname>
            <nickname>fong fong</nickname>
            <salary>200000</salary>
        </staff>
    </company>

2 个答案:

答案 0 :(得分:0)

如果您只关心这些值,则创建Java POJO以使用Jackson解析XML。让框架完成所有繁重的工作。

public class Company
{
    private Staff[] staff;

    public Staff[] getStaff ()
    {
        return staff;
    }

    public void setStaff (Staff[] staff)
    {
        this.staff = staff;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [staff = "+staff+"]";
    }
}

public class Staff
{
    private String id;

    private String nickname;

    private String lastname;

    private String firstname;

    private String salary;

    public String getId ()
    {
        return id;
    }

    public void setId (String id)
    {
        this.id = id;
    }

    public String getNickname ()
    {
        return nickname;
    }

    public void setNickname (String nickname)
    {
        this.nickname = nickname;
    }

    public String getLastname ()
    {
        return lastname;
    }

    public void setLastname (String lastname)
    {
        this.lastname = lastname;
    }

    public String getFirstname ()
    {
        return firstname;
    }

    public void setFirstname (String firstname)
    {
        this.firstname = firstname;
    }

    public String getSalary ()
    {
        return salary;
    }

    public void setSalary (String salary)
    {
        this.salary = salary;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [id = "+id+", nickname = "+nickname+", lastname = "+lastname+", firstname = "+firstname+", salary = "+salary+"]";
    }
}

然后,您可以使用以下命令填充Company POJO:

Company company = objectMapper.readValue(jsonFileAbove, Company.class);

答案 1 :(得分:0)

您可能要使用XPath

XPath xpath = XPathFactory.newInstance().newXPath();
NodeList staffElements = (NodeList)
    xpath.evaluate("/company/staff", doc, XPathConstants.NOESET);

int count = staffElements.getLength();
for (int i = 0; i < count; i++) {
    Element staffElement = (Element) staffElements.item(i);

    System.out.println("Salary : "
        + staffElement.getElementsByTagName("salary")
            .item(0).getTextContent());

    NodeList staffChildren = + staffElement.getElementsByTagName("staff");
    if (staffChildren.getLength() > 0) {
        System.out.println("Staff count : "
            + staffChildren.item(0).getTextContent());
    }
}