将xml转换为java bean [英] Convert xml to java bean

查看:127
本文介绍了将xml转换为java bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将xml文件转换为简单的java bean?
它是一个没有任何xsd的简单xml文件,它是从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.

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

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.

推荐答案

JAXB

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

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

  • Metro JAXB (the reference implementation included in Java SE 6)
  • EclipseLink JAXB (MOXy), I'm the tech lead
  • Apache JaxMe

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

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;

}

将对应以下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的地理编码格式:

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;

}

上述类对应于以下XML:

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> 

更多信息

  • XPath Based Mapping - Geocode Example
  • Map to Element based on an Attribute Value with EclipseLink JAXB (MOXy)
  • XPath Based Mapping

这篇关于将xml转换为java bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆