如何解组此XML文件结构

时间:2015-06-24 09:11:34

标签: java xml jaxb

我将如何使用JAXB解组此XML文件结构:

<document>
  <properties>
    <basic>
      <property id="generationDate">
        <value>20150525</value>
      </property>
      <property id="hostAddress">
        <value>192.168.0.250</value>
        </property>
    </basic>
  </properties>
</document>

Java类的片段

import javax.xml.bind.annotation.*;

    @XmlRootElement(name = "document")
    @XmlAccessorType(XmlAccessType.FIELD)
    class PDFDocument {

        @XmlID
        @XmlAttribute(name = "generationDate")    
        private String generationDate;
来自Unmarshalling代码的

片段:

 PDFDocument doc = new PDFDocument();
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance(PDFDocument.class);
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                doc = (PDFDocument) jaxbUnmarshaller.unmarshal(new File(filePath));

            } catch (JAXBException ex) {
                Logger.getLogger(FileFunctions.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println(doc.getGenerationDate());

但我不确定如何引用某个属性的每个值。

1 个答案:

答案 0 :(得分:0)

好的,基本上你的Java对象结构是错误的。您需要分析XML,然后需要相应地构建Java Object结构。

如果您查看XML,您有一个Document,并且在Document中有属性,在属性内有基本属性等等。

所以类似地,你需要拥有java类,一个文档类,你需要在Basic文档中使用属性类,等等。我已经在下面展示了类结构,这样你就可以得到一个想法。

文件类 -

@XmlRootElement(name = "document")
@XmlAccessorType(XmlAccessType.FIELD)
public class PDFDocument {

@XmlElement(name = "properties")
private DocumentProperty documentProperty;

public DocumentProperty getDocumentProperty() {
    return documentProperty;
}

public void setDocumentProperty(DocumentProperty documentProperty) {
    this.documentProperty = documentProperty;
}

@Override
public String toString() {
    return "PDFDocument{" +
            "documentProperty=" + documentProperty + "\n" +
            '}';
}
}

要保留属性的类 -

@XmlRootElement(name = "properties")
@XmlAccessorType(XmlAccessType.FIELD)
public class DocumentProperty {

@XmlElement(name = "basic")
private Basic basic;

public Basic getBasic() {
    return basic;
}

public void setBasic(Basic basic) {
    this.basic = basic;
}

@Override
public String toString() {
    return "DocumentProperty{" +
            "basic=" + basic + "\n" +
            '}';
}
}

基础班 -

@XmlRootElement(name = "basic")
@XmlAccessorType(XmlAccessType.FIELD)
public class Basic {

@XmlElementRef
private List<Property> propertyList;

public List<Property> getPropertyList() {
    return propertyList;
}

public void setPropertyList(List<Property> propertyList) {
    this.propertyList = propertyList;
}

@Override
public String toString() {
    return "Basic{" +
            "propertyList=" + propertyList + "\n" +
            '}';
}
}

属性类 -

@XmlRootElement(name = "property")
@XmlAccessorType(XmlAccessType.FIELD)
public class Property {

@XmlAttribute(name = "id")
private String id;

@XmlElement(name = "value")
private String value;

public String getId() {
    return id;
}

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

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

@Override
public String toString() {
    return "Property{" +
            "id='" + id + "\n" +
            ", value='" + value + "\n" +
            '}';
}
}

Above Class结构与您提供的XML一起使用。但我想你必须改变这个结构,因为除了Basic Type之外,Document中还有其他类型的属性。在那种情况下,我建议你有抽象的Document Property类,并将它扩展到Basic和其他类型的Properties。

测试类 -

public class XmlTest {

@Test
public void testXml() throws Exception {

    String xml = "<document>" +
            "  <properties>" +
            "    <basic>" +
            "      <property id=\"generationDate\">" +
            "        <value>20150525</value>" +
            "      </property>\n" +
            "      <property id=\"hostAddress\">" +
            "        <value>192.168.0.250</value>" +
            "        </property>" +
            "    </basic>" +
            "  </properties>" +
            "</document>";

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(PDFDocument.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        PDFDocument document = (PDFDocument) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes()));


        System.out.println("PDF Document Structure -" +document.toString());


        for(Property property : document.getDocumentProperty().getBasic().getPropertyList()) {
            if(property.getId().equals("generationDate")){
                System.out.println("Generation Date : "+property.getValue());
            }
        }
    } catch (JAXBException ex) {
        ex.printStackTrace();
    }
}
}

运行Test类生成以下结果。

PDFDocument{
documentProperty=
DocumentProperty{
basic=Basic{
propertyList=[
Property{
id='generationDate
, value='20150525
}, 
Property{id='hostAddress
, value='192.168.0.250
}]
}
}
}

希望这有助于。