将 xml 转换为 java bean

发布于 2024-11-04 07:53:27 字数 163 浏览 1 评论 0原文

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

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

How can I covert a an xml file to a simple java bean?
Its a simple xml file without any xsd, which was generated from a java bean, which I don't have access to.

I tried using xmlbeans to first generate the xmd from xml and then to generate classes from the xsd. I got a bunch of classes. I am looking for a single java bean class.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人生百味 2024-11-11 07:53:28

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

You could use a tool like Castor or JAXB to map the XML to a java class. Castor is fairly easy to use.

客…行舟 2024-11-11 07:53:27

JAXB

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

  • Metro JAXB(Java SE 6 中包含的参考实现)
  • < a href="http://www.eclipse.org/eclipselink/moxy.php" rel="noreferrer">EclipseLink JAXB (MOXy),我是技术主管
  • Apache JaxMe

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> 

有关详细信息

JAXB

JAXB (JSR-222) provides an easy way to convert objects to XML. There are many open source implementations of this standard including:

JAXB has a default mapping for Java objects to XML. This mapping can be customized through the application of annotations.

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;

}

Would correspond to the following 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 has an XPath based mapping extension. This means we can take our same Address class and map it to Google's geocode format:

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;

}

The above class corresponds to the following 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> 

For more Information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文