Java 如何解组此XML文件结构

Java 如何解组此XML文件结构,java,xml,jaxb,Java,Xml,Jaxb,我将如何使用JAXB来解组此XML文件结构: <document> <properties> <basic> <property id="generationDate"> <value>20150525</value> </property> <property id="hostAddress"> <value>

我将如何使用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>
解组代码的片段:

 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());

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

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

如果您查看XML,您有一个文档,文档中有属性,属性中有基本属性,等等

所以类似地,您需要有java类,一个文档类,在文档中,您需要有properties类,在基本类中,等等。我已经在下面展示了班级结构,这样你就可以有一个想法了

文件类别-

@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" +
            '}';
}
}
上面的类结构适用于您提供的XML。但我想您必须更改此结构,因为文档中除了基本类型之外还有其他类型的属性。在这种情况下,我建议您使用抽象文档属性类,并将其扩展到基本属性和其他类型的属性

测试类别-

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();
    }
}
}

希望这有帮助

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

如果您查看XML,您有一个文档,文档中有属性,属性中有基本属性,等等

所以类似地,您需要有java类,一个文档类,在文档中,您需要有properties类,在基本类中,等等。我已经在下面展示了班级结构,这样你就可以有一个想法了

文件类别-

@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" +
            '}';
}
}
上面的类结构适用于您提供的XML。但我想您必须更改此结构,因为文档中除了基本类型之外还有其他类型的属性。在这种情况下,我建议您使用抽象文档属性类,并将其扩展到基本属性和其他类型的属性

测试类别-

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();
    }
}
}

希望这有帮助

太好了,我不确定班级结构,所以你的答案很有帮助。它现在已经实现并像一个符咒一样工作;-)太好了,我不确定班级结构,所以你的答案很有帮助。它现在已经实现并像一个符咒一样工作;-)