如何使用Java中没有属性值的多组元素读取XML文件?

时间:2015-12-18 05:43:56

标签: java xml xml-parsing

  

我基本上想知道如何从xml中获取这些元素值   java中的文件将其设置为Object。

以下是示例XML文件

<div ng-repeat-start="item in items" ng-controller="ItemCtrl as ctrl" ng-hide="ctrl.isEditing">
    <span>Name: {{item.name}}.</span>
    <a href='#' ng-click='ctrl.startEditing()'>Edit</a>
</div>

<div ng-repeat-end ng-show="ctrl.isEditing">
    <input type='text' ng-model='item.name'/>
    <a href='#' ng-click='ctrl.save()'>Save</a>
</div>

以下是我想要获取xml数据的类......

<?xml version="1.0" encoding="UTF-8"?>
<Response>
   <LoanApplication>
      <LoanId>1111</LoanId>
      <LoanStatus>Unsuccessful</LoanStatus>
      <LoanReference>111</LoanReference>
   </LoanApplication>
   <LoanApplication>
      <LoanId>222</LoanId>
      <LoanStatus>Unsuccessful</LoanStatus>
      <LoanReference>333</LoanReference>
   </LoanApplication>
   <LoanApplication>
      <LoanId>222</LoanId>
      <LoanStatus>Unsuccessful</LoanStatus>
      <LoanReference>4444</LoanReference>
   </LoanApplication>
   <LoanApplication>
      <LoanId>555</LoanId>
      <LoanStatus>Current</LoanStatus>
      <LoanReference>7777</LoanReference>
   </LoanApplication>
   <LoanApplication>
      <LoanId>3333</LoanId>
      <LoanStatus>Current</LoanStatus>
      <LoanReference>9999</LoanReference>
   </LoanApplication>
</Response>   

以下是为解析xml文件付出一些努力的方法。现在,我很震惊如何设置我的课程设置者。

public class LoanIDResponse implements java.io.Serializable
{
private Integer loanID;
private Integer loanReference;
private String loanStatus;

public LoanIDResponse()
{
}
public void setLoanID(Integer loanID)
{
    this.loanID = loanID;
}

public void setLoanStatus(String loanStatus)
{
    this.loanStatus = loanStatus;
}

public void setLoanReference(Integer loanReference)
{
    this.loanReference = loanReference;
}
public Integer getLoanID()
{
    return loanID;
}

public Integer getLoanReference()
{
    return loanReference;
}

public String getLoanStatus()
{
    return loanStatus;
}
}

2 个答案:

答案 0 :(得分:1)

首先查看Introduction to JAXBJava API for XML Processing (JAXP)了解更多详情。

此示例适用于JAXB

首先,您需要一些类来保存数据

LoanApplication

这是贷款申请的基本属性

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "LoanApplication")
public class LoanApplication {
    private long loanId;
    private String loanStatus;
    private long loadReference;

    public long getLoanId() {
        return loanId;
    }

    public String getLoanStatus() {
        return loanStatus;
    }

    public long getLoadReference() {
        return loadReference;
    }

    @XmlElement(name="LoanId")
    public void setLoanId(long loanId) {
        this.loanId = loanId;
    }

    @XmlElement(name="LoanStatus")
    public void setLoanStatus(String loanStatus) {
        this.loanStatus = loanStatus;
    }

    @XmlElement(name="LoanReference")
    public void setLoadReference(long loadReference) {
        this.loadReference = loadReference;
    }

    @Override
    public String toString() {
        return getLoanId() + "; " + getLoanStatus() + "; " + getLoadReference();
    }
}

响应

这是一个“容器”类,其中包含LoanApplications

列表
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Response")
public class Response {

    private List<LoanApplication> applications;

    @XmlElement(name="LoanApplication")
    public void setApplications(List<LoanApplication> applications) {
        this.applications = applications;
    }

    public List<LoanApplication> getApplications() {
        return applications;
    }

}

阅读...

然后我们可以使用类似......

之类的东西来读取XML文件
try {
    File file = new File("Responses.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Response response = (Response) jaxbUnmarshaller.unmarshal(file);
    for (LoanApplication app : response.getApplications()) {
        System.out.println(app);
    }
} catch (JAXBException ex) {
    ex.printStackTrace();
}

基于我的示例代码,输出

1111; Unsuccessful; 111
222; Unsuccessful; 333
222; Unsuccessful; 4444
555; Current; 7777
3333; Current; 9999

书写...

写作与阅读几乎一样,事实上,对于这个例子,它也包括阅读部分,所以我们可以更新内容...

try {
    File file = new File("Responses.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Response response = (Response) jaxbUnmarshaller.unmarshal(file);
    for (LoanApplication app : response.getApplications()) {
        System.out.println(app);
    }

    LoanApplication app = new LoanApplication();
    app.setLoadReference(123456);
    app.setLoanId(7890);
    app.setLoanStatus("Approved");
    response.getApplications().add(app);

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(response, new File("Responses.xml"));
    //jaxbMarshaller.marshal(response, System.out);
} catch (JAXBException ex) {
    ex.printStackTrace();
}

哪个生成...

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
    <LoanApplication>
        <LoanReference>111</LoanReference>
        <LoanId>1111</LoanId>
        <LoanStatus>Unsuccessful</LoanStatus>
    </LoanApplication>
    <LoanApplication>
        <LoanReference>333</LoanReference>
        <LoanId>222</LoanId>
        <LoanStatus>Unsuccessful</LoanStatus>
    </LoanApplication>
    <LoanApplication>
        <LoanReference>4444</LoanReference>
        <LoanId>222</LoanId>
        <LoanStatus>Unsuccessful</LoanStatus>
    </LoanApplication>
    <LoanApplication>
        <LoanReference>7777</LoanReference>
        <LoanId>555</LoanId>
        <LoanStatus>Current</LoanStatus>
    </LoanApplication>
    <LoanApplication>
        <LoanReference>9999</LoanReference>
        <LoanId>3333</LoanId>
        <LoanStatus>Current</LoanStatus>
    </LoanApplication>
    <LoanApplication>
        <LoanReference>123456</LoanReference>
        <LoanId>7890</LoanId>
        <LoanStatus>Approved</LoanStatus>
    </LoanApplication>
</Response>

注意最后一条记录是新的

我几乎从this examplethis example

一起蹒跚而行

答案 1 :(得分:1)

您可以获得类似...的节点值。

 NodeList loanIDList = loanApplicationElement.getElementsByTagName("LoanId");
 Element loanIDElement = (Element) loanIDList.item(0);

 NodeList textIDList = loanIDElement.getChildNodes();
 response.setLoanID(Integer.parseInt(((Node)textIDList.item(0)).getNodeValue().trim()));

//-------
 NodeList loanReferenceList = loanApplicationElement.getElementsByTagName("LoanReference");
 Element loanReferenceElement = (Element) loanReferenceList.item(0);

 NodeList textLRList = loanReferenceElement.getChildNodes();
 response.setLoanReference(Integer.parseInt(((Node) textLRList.item(0)).getNodeValue().trim()));
//-------
 NodeList loanStatusList = loanApplicationElement.getElementsByTagName("LoanStatus");
 Element loanStatusElement = (Element) loanStatusList.item(0);

 NodeList textLSList = loanStatusElement.getChildNodes();
 response.setLoanStatus(((Node) textLSList.item(0)).getNodeValue().trim());