7

我有以下 xml 字符串。我想将其转换为 java 对象,以将每个标签与该对象的字段映射。与标签名称相比,如果我可以引入不同的字段名称,那就更好了。我怎么能这样做?我正在查看 JAXB,但我仍然对“ns4:response”之类的部分和标签中的标签感到困惑。先感谢您...

<ns4:response>
    <count>1</count>
    <limit>1</limit>
    <offset>1</offset>
    <ns3:payload xsi:type="productsPayload">
        <products>
            <product>
                <avgRating xsi:nil="true"/>
                <brand>Candie's</brand>
                <description>
                    <longDescription>
                    long descriptions
                    </longDescription>
                    <shortDescription>
                    short description
                    </shortDescription>
                </description>
                <images>
                    <image>
                        <altText>alternate text</altText>
                        <height>180.0</height>
                        <url>
                        url
                        </url>
                        <width>180.0</width>
                    </image>
                </images>
                <price>
                    <clearancePrice xsi:nil="true"/>
                    <regularPrice xsi:nil="true"/>
                    <salePrice>28.0</salePrice>
                </price>
            </product>
        </products>
    </ns3:payload>
</ns4:response>
4

4 回答 4

27

JAXB 是用于将对象与 XML 相互转换的 Java 标准 ( JSR-222 )。以下内容应该有所帮助:

从字符串中解组

在您的 JAXB impl 可以解组它之前,您需要将其包装String在一个实例中。StringReader

StringReader sr = new StringReader(xmlString);
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Response response = (Response) unmarshaller.unmarshal(sr);

不同的字段和 XML 名称

您可以使用@XmlElement注释来指定您想要的元素名称。默认情况下,JAXB 查看属性。如果您希望将映射基于字段,则需要设置@XmlAccessorType(XmlAccessType.FIELD).

@XmlElement(name="count")
private int size;

命名空间

和注释@XmlRootElement@XmlElement允许您在需要时指定命名空间限定。

@XmlRootElement(namespace="http://www.example.com")
public class Response {
}

了解更多信息

于 2012-08-10T11:20:57.367 回答
2

JAXB 是一个不错的选择。如果您有此文档的 XSD 文件,这将非常容易。JAXB 可以为特定模式生成 Java 代码。如果您没有 XSD 文件,则需要自己准备 Java 类。查找 JAXB 教程并查看文档http://jaxb.java.net/tutorial/。标签中的标签只是 JAXB 的嵌套对象。ns4是一个命名空间。JAXB 支持名称空间 - 只需在文档中查找即可。您可以使用注释来引入与 XML 中的标签不同的字段名称。遵循文档。

于 2012-08-10T08:06:08.907 回答
1

如果您有上面显示的 XML 的 XSD。
我建议您使用 Jaxb。
JAXB 从 XML 文件创建 java 对象。您需要首先使用 jaxb 的代码生成器生成 Java 类,该代码生成器将 XSD 作为输入,然后适当地序列化/反序列化这些 xml 文件。

于 2012-08-10T07:58:31.363 回答
1

如果你已经有了 xml,并且有多个属性,你可以按如下方式处理它:

    String output = "<ciudads><ciudad><idCiudad>1</idCiudad>
    <nomCiudad>BOGOTA</nomCiudad></ciudad><ciudad><idCiudad>6</idCiudad>
    <nomCiudad>Pereira</nomCiudad></ciudads>";
    DocumentBuilder db = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(output));

    Document doc = db.parse(is);
    NodeList nodes = ((org.w3c.dom.Document) doc)
        .getElementsByTagName("ciudad");

    for (int i = 0; i < nodes.getLength(); i++) {           
        Ciudad ciudad = new Ciudad();
        Element element = (Element) nodes.item(i);

        NodeList name = element.getElementsByTagName("idCiudad");
        Element element2 = (Element) name.item(0);
        ciudad.setIdCiudad(Integer
            .valueOf(getCharacterDataFromElement(element2)));

        NodeList title = element.getElementsByTagName("nomCiudad");
        element2 = (Element) title.item(0);
        ciudad.setNombre(getCharacterDataFromElement(element2));

        ciudades.getPartnerAccount().add(ciudad);
    }
    }

    for (Ciudad ciudad1 : ciudades.getPartnerAccount()) {
    System.out.println(ciudad1.getIdCiudad());
    System.out.println(ciudad1.getNombre());
    }

getCharacterDataFromElement 方法是

    public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
    CharacterData cd = (CharacterData) child;

    return cd.getData();
    }
    return "";
    }
于 2016-12-09T16:34:38.420 回答