将xml转换为java bean

时间:2011-04-29 04:05:14

标签: java xml javabeans xmlbeans

如何将xml文件转换为简单的java bean? 它是一个简单的xml文件,没有任何xsd,它是从java bean生成的,我无法访问它。

我尝试使用xmlbeans首先从xml生成xmd,然后从xsd生成类。我有一堆课。我正在寻找一个单独的java bean类。

3 个答案:

答案 0 :(得分:13)

<强> JAXB

JAXB(JSR-222)提供了一种将对象转换为XML的简便方法。该标准有许多开源实现,包括:

JAXB具有Java对象到XML的默认映射。可以通过应用注释来自定义此映射。

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

@XmlRootElement
public class Address {

    private String street;

    private String city;

    private String state;

    private String country;

    @XmlElement(name="postal-code")
    private String postalCode;

}

将对应于以下XML:

<address>
    <street>123 A Street</street>
    <city>Any Town</city>
    <state>A State</state>
    <postal-code>12345</postal-code>
</address>

EclipseLink JAXB(MOXy)

MOXy具有基于XPath的映射扩展。这意味着我们可以使用相同的Address类并将其映射到Google的地理编码格式:

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

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name="kml")
@XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
public class Address {

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
    private String street;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
    private String city;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
    private String state;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
    private String country;

    @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
    private String postalCode;

}

上述类对应于以下XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0" xmlns:ns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
    <Response>
        <Placemark>
            <ns:AddressDetails>
                <ns:Country>
                    <ns:CountryNameCode>US</ns:CountryNameCode>
                    <ns:AdministrativeArea>
                        <ns:AdministrativeAreaName>CA</ns:AdministrativeAreaName>
                        <ns:SubAdministrativeArea>
                            <ns:Locality>
                                <ns:LocalityName>Mountain View</ns:LocalityName>
                                <ns:Thoroughfare>
                                    <ns:ThoroughfareName>1600 Amphitheatre Pkwy</ns:ThoroughfareName>
                                </ns:Thoroughfare>
                                <ns:PostalCode>
                                    <ns:PostalCodeNumber>94043</ns:PostalCodeNumber>
                                </ns:PostalCode>
                            </ns:Locality>
                        </ns:SubAdministrativeArea>
                    </ns:AdministrativeArea>
                </ns:Country>
            </ns:AddressDetails>
        </Placemark>
    </Response>
</kml> 

了解更多信息

答案 1 :(得分:0)

尝试Castor Mapping

答案 2 :(得分:-1)

您可以使用Castor或JAXB等工具将XML映射到java类。 Castor相当容易使用。