如何使用JAXB在Java中解析此XML?

时间:2011-11-18 18:03:47

标签: java xml jaxb

我有以下XML,没有XSD或者我想要使用JAXB解析为java对象的模式,因为我听说它比SAX更好。有没有办法用注释或更好的方法来实现这一点?它是否使我只需要一个FosterHome类?我很难找到如何做到这一点任何帮助都很感激。

我原本打算参加FosterHome,Family和Child课程。使用JAXB,不再需要3个类?我不知道如何解决这个问题,因为ChildID出现在两个不同的领域。

<?xml version="1.0" encoding="UTF-8"?>
<FosterHome>
    <Orphanage>Happy Days Daycare</Orphanage>
    <Location>Apple Street</Location>
    <Families>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child1</ChildID>
                <ChildID>Child2</ChildID>
            </ChildList>
        </Family>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child3</ChildID>
                <ChildID>Child4</ChildID>
            </ChildList>
        </Family>
    </Families>
    <RemainingChildList>
        <ChildID>Child5</ChildID>
        <ChildID>Child6</ChildID>
    </RemainingChildList>
</FosterHome>

3 个答案:

答案 0 :(得分:59)

您可以执行以下操作。通过利用@XmlElementWrapper,您可以减少所需的课程数量:

<强> FosterHome

package nov18;

import java.util.List;
import javax.xml.bind.annotation.*;

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

    @XmlElement(name="Orphanage")
    private String orphanage;

    @XmlElement(name="Location")
    private String location;

    @XmlElementWrapper(name="Families")
    @XmlElement(name="Family")
    private List<Family> families;

    @XmlElementWrapper(name="RemainingChildList")
    @XmlElement(name="ChildID")
    private List<String> remainingChildren;

}

<强>家庭

package nov18;

import java.util.List;   
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Family {

    @XmlElement(name="ParentID")
    private String parentID;

    @XmlElementWrapper(name="ChildList")
    @XmlElement(name="ChildID")
    private List<String> childList;

}

<强>演示

package nov18;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(FosterHome.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        FosterHome fosterHome = (FosterHome) unmarshaller.unmarshal(new File("src/nov18/input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(fosterHome, System.out);
    }

}

<强>输入/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FosterHome>
    <Orphanage>Happy Days Daycare</Orphanage>
    <Location>Apple Street</Location>
    <Families>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child1</ChildID>
                <ChildID>Child2</ChildID>
            </ChildList>
        </Family>
        <Family>
            <ParentID>Adams</ParentID>
            <ChildList>
                <ChildID>Child3</ChildID>
                <ChildID>Child4</ChildID>
            </ChildList>
        </Family>
    </Families>
    <RemainingChildList>
        <ChildID>Child5</ChildID>
        <ChildID>Child6</ChildID>
    </RemainingChildList>
</FosterHome>

了解更多信息


<强>更新

  

我是否可以轻松地迭代/打印出所有的ChildID   家庭班?

您可以执行以下操作:

    for(Family family : fosterHome.getFamilies()) {
        System.out.println(family.getParentID());
        for(String childID : family.getChildList()) {
            System.out.println("    " + childID);
        }
    }

答案 1 :(得分:3)

try {
    // create a JAXBContext capable of handling classes generated into
    // the com.abhi.xml.jaxb.generated package
    JAXBContext jc = JAXBContext.newInstance( "com.abhi.xml.jaxb.generated" );

    // create an Unmarshaller
    Unmarshaller u = jc.createUnmarshaller();

    // unmarshal a FosterHome instance document into a tree of Java content
    // objects composed of classes from the com.abhi.xml.jaxb.generated 
    // package.
    JAXBElement<?> fhElement =(JAXBElement<?>)u.unmarshal
    (new FileInputStream("yourfile.xml"));
    FosterHome FH = (FosterHome)fhElement.getValue();
    System.out.println(FH.getDesc());
         // so on ..you can get all elements based on generated objects

} catch( JAXBException je ) {
    je.printStackTrace();
} catch( IOException ioe ) {
    ioe.printStackTrace();
}

答案 2 :(得分:1)

这似乎是一个很好的教程

http://blog.espenberntsen.net/2010/02/26/generate-jaxb-classes-from-an-xsd-schema-and-the-schema-from-an-xml-sample-document/

详细介绍了如何从xml文件生成xsd模式,然后介绍如何使用该模式来生成jaxb类。最后,你最终应该有一个以上的课程。