解析Java中未格式化的XML并转换为对象

时间:2019-01-11 04:19:42

标签: java xml

我们有一个用Java完成的应用程序。我们需要解析未格式化的XML并将其转换为Java对象。

在Java中做这件事的最好方法是什么?

谢谢

<category>
    <pattern>WHAT IS MY NAME</pattern>
    <template>
        Your name is <get name="NAME" />.

        <condition name="PHONE">
            <li value="unknown">
                <condition name="EMAIL">
                    <li value="unknown">I cannot find the contact details for the name.</li>
                    <li>You can email him/her at <get name="EMAIL" />.</li>
                </condition>
            </li>
            <li>
                <condition name="EMAIL">
                    <li value="unknown">You can call him/her on <get name="PHONE" />.</li>
                    <li>You can call them on <get name="PHONE" /> or email them at <get name="EMAIL" />.</li>
                </condition>
            </li>
        </condition>
    </template>
</category>
<category>
    <pattern>WHO IS MY PARENT</pattern>
    <template>
        <templatetext>
        Your parent is $get[PARENT_CONTACT_NAME].
        You can email him/her at $get[PARENT_EMAIL].
        His/her phone number is $get[PARENT_PHONE].
        </templatetext>
    </template>
</category>

2 个答案:

答案 0 :(得分:1)

对于这种复杂的xml结构,我认为解析的最佳方法是编写一个xsd模式,然后将xml转换为java对象。

下面的示例不会为您提供问题的确切解决方案,而只是提供使用JAXB将xml解析为Java Object的想法。 显然,该示例也不适用于这种复杂的xml,但是一旦您为相同的XML编写了xsd模式,该示例可能会有所帮助:

这是employee.xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <fName>John</fName>
    <id>1</id>
    <lName>Paul</lName>
    <domain>
        <id>999</id>
        <name>CS</name>
    </domain>
</employee>

Employee.java和Domain.java:

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String fName;
    private String lName;
    private Domain domain;

    public Employee() {
        super();
    }

    public Employee(int id, String fName, String lName, Domain domain) {
        super();
        this.id = id;
        this.fName = fName;
        this.lName = lName;
        this.domain = domain;
    }

    //Setters and Getters

    public Integer getId() {
        return id;
    }

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

    public String getfName() {
        return fName;
    }

    public void setfName(String fName) {
        this.fName = fName;
    }

    public String getlName() {
        return lName;
    }

    public void setlName(String lName) {
        this.lName = lName;
    }

    public Domain getDomain() {
        return domain;
    }

    public void setDomain(Domain domain) {
        this.domain = domain;
    }



    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + fName + ",lastName=" + lName + 
", department="+ domain + "]";
    }
}  

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

@XmlRootElement(name = "domain")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Domain implements Serializable {

    private static final long serialVersionUID = 1L;


    Integer id;
    String name;

    public Domain() {
        super();
    }

    public Domain(Integer id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    //Setters and Getters
    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    @Override
    public String toString() {
        return "Domain [id=" + id + ", name=" + name + "]";
    }
}

JAXB将XML文件解析为Java:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;

public class ParseXMLFile {


    public static void main(String[] args) {

        File xmlFile = new File("src/employee.xml");

        JAXBContext jaxbContext;
        try
        {
            jaxbContext = JAXBContext.newInstance(Employee.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            Employee employee = (Employee) jaxbUnmarshaller.unmarshal(xmlFile);
            System.out.println(employee);
        }
        catch (JAXBException e)
        {
            e.printStackTrace();
        }
    }


}

输出:

Employee [id=1, firstName=John,lastName=Paul, department=Domain [id=999, name=CS]]

希望您对这些事情有个了解。

答案 1 :(得分:0)

这似乎是“半结构化”数据的经典示例-介于纯数据和纯文档之间。 XML处理半结构化数据的能力比Java好得多,而像XSLT这样的面向XML的编程语言在处理此类数据方面比Java更好。那么将数据输入Java真的朝着正确的方向迈出了一步?