如果子元素的模式未知,如何解组嵌套的子元素(包含<&>)。我不能用< >在xml中。

时间:2015-09-02 10:51:26

标签: java xml-parsing jaxb unmarshalling

客户架构:

@XmlRootElement
public class Customer {

String name;
int id;

public String getName() {
    return name;
}

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

public int getId() {
    return id;
}

@XmlElement
public void setId(int id) {
    this.id = id;
}

}

解组编码:

public static void unmarshalXml2Obj() {

    try {

        File file = new File(
                "D:/Workspace/CustomerFile.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);

        System.out.println(customer);
        System.out.println("ID : " + customer.getId());
        System.out.println("Name : " + customer.getName());
        System.out.println("-------------------------");

    } catch (JAXBException e) {
        e.printStackTrace();
    }

}

要解组的XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer >
  <id>100</id>
  <name><FN>Johny</FN><LN>Depp</LN><a>AAAA</a><z>ZZZZ</z></name>
</customer>

输出:

test.example.Customer@eeb406
ID : 100
Name : 
-------------------------

不使用尖括号时:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer >
  <id>100</id>
  <name>&lt;FN&gt;Johny&lt;/FN&gt;&lt;LN&gt;Depp&lt;/LN&gt;&lt;a&gt;AAAA&lt;/a&gt;&lt;z&gt;ZZZZ&lt;/z&gt;</name>
</customer>

正确输出:

test.example.Customer@7a279c
ID : 100
Name : <FN>Johny</FN><LN>Depp</LN><a>AAAA</a><z>ZZZZ</z>
-------------------------

我无法在我的xml中使用&amp; lt&amp; gt。需要使用Angular括号(&lt;和&gt;)的解决方案。

0 个答案:

没有答案