使用Java

时间:2016-06-06 15:43:13

标签: java xml

我目前正在尝试弄清楚如何从XML标记中获取信息并存储它。我可以通过名字对数据进行排序。到目前为止,我已经到了我的程序将解析数据并读取它的地方。但我无法弄清楚如何实际存储数据。 我无法弄清楚如何从子标签中获取信息,所以非常感谢所有帮助。

<?xml version="1.0" encoding="UTF-8"?>
<abc targetNamespace="http://www.example.com/abcd">
<abcd>
    <employeeList>
        <employee>
            <name>
                <last>Bob</last>
                <first>John</first>
            </name>
            <title>Merchant</title>
            <phone>9987583687</phone>
        </employee>

        <employee>
            <name>
                <last>Roy</last>

1 个答案:

答案 0 :(得分:2)

您是否考虑过使用JAXB(Java Architecture for Xml Binding)?

有一系列的教程可用,例如: https://docs.oracle.com/javase/tutorial/jaxb/intro/examples.html

Blaise Doughan在他的博客和StackOverflow帐户上也有一些关于JAXB的优秀资源:http://blog.bdoughan.com/

作为快速介绍,我根据您在问题中提供的xml做了一个示例。假设您有想要存储xml信息的域对象,可以使用注释使它们兼容:

import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement
public class EmployeeList {
    private List<Employee> employees = new ArrayList<>();

    public EmployeeList(){}

    public EmployeeList(List<Employee> employees){
        this.employees = employees;
    }

    public List<Employee> getEmployees(){ return this.employees; }
    public void setEmployees(List<Employee> employees){ this.employees = employees; }
}

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
    private Name name;
    private String title, phoneNumber;

    public Employee(){ }

    public Employee(Name name, String title, String phoneNumber){
        this.name = name;
        this.title = title;
        this.phoneNumber = phoneNumber;
    }

    public Name getName(){ return this.name; }
    public String getTitle(){ return this.title; }
    public String getPhoneNumber(){ return this.phoneNumber; }

    public void setName(Name name){ this.name = name; }
    public void setTitle(String title){ this.title = title; }
    public void setPhoneNumber(String phoneNumber){ this.phoneNumber = phoneNumber; }
}

public class Name {
    private String firstName, lastName;

    public Name(){ }

    public Name(String firstName, String lastName){
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName(){ return this.firstName; }
    public String getLastName(){ return this.lastName; }

    public void setFirstName(String firstName){ this.firstName = firstName; }
    public void setLastName(String lastName){ this.lastName = lastName; }
}

这些小的更改将允许您将对象转换为xml

转换为Xml的示例

        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(new Name("John", "Doe"), "Assistant", "123"));
        employees.add(new Employee(new Name("Jane", "Doe"), "Merchant", "456"));
        EmployeeList employeeList = new EmployeeList(employees);

        try {
            File outputFile = new File("employeeListExample.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeList.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            jaxbMarshaller.marshal(employeeList, outputFile);
        } catch (PropertyException exception) {
            exception.printStackTrace();
        } catch (JAXBException exception) {
            exception.printStackTrace();
        }

将提供以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employeeList>
    <employees>
        <name>
            <firstName>John</firstName>
            <lastName>Doe</lastName>
        </name>
        <phoneNumber>123</phoneNumber>
        <title>Assistant</title>
    </employees>
    <employees>
        <name>
            <firstName>Jane</firstName>
            <lastName>Doe</lastName>
        </name>
        <phoneNumber>456</phoneNumber>
        <title>Merchant</title>
    </employees>
</employeeList>

注意:由于员工也是根元素,因此也可以通过更改使用的类实例来编组单个员工

从Xml转换的示例:

     File outputFile = new File("employeeListExample.xml");
     JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeList.class);
     Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
     EmployeeList list = (EmployeeList)jaxbUnmarshaller.unmarshal(outputFile);

使用上面创建的xml并进行一些小的更改,我们可以将xml转换回用于创建它的EmployeeList(仍需要try / catch)

<小时/> 使用这种方法,您就可以将排序应用于未编组列表。

虽然这个答案并不能完美地匹配你的xml,但希望它会给你一些东西来试验