使用JAXB将子节点中的XML数据作为字符串获取 [英] Getting a XML data present in a child node as string using JAXB

查看:198
本文介绍了使用JAXB将子节点中的XML数据作为字符串获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB将XML(Parent XML,FIXML)转换为String。在该XML中,一个子节点具有另一个XML(子XML,FpML)数据,我已经为其创建了模式(xsd)。在Parent XML的模式中,具有Child XML的元素类型被定义为String。我需要孩子XML应该作为字符串。请让我知道我必须做些什么改变。我对JAXB比较陌生。在此先感谢.. !!

I am using JAXB to convert the XML(Parent XML, FIXML) to String. In that XML one child node is having another XML(Child XML, FpML) data for which I done have schema (xsd). In the schema for the Parent XML, the element type which has the Child XML is defined as String. I need the child XML should come as the string. Please let me know what change I have to do. I am relatively new to JAXB. Thanks in Advance..!!

干杯,
Sakthi S

Cheers, Sakthi S

推荐答案

更新

根据您的评论:


嗨..感谢您的回复.. @Blaise
Doughan&在你提供的上面
示例中的@CoolBeans你提到
字符串值但是在我的
xml中,而不是字符串值,
将是其中的另一个XML。比如
Richard
我需要输出
Richard作为
字符串。请告诉我
仍然需要更多信息。
谢谢..干杯,Sakthi。 S

Hi.. Thanks for your reply.. @Blaise Doughan & @CoolBeans in the above example you provide you have mentioned String Value but in my xml, instead of "String Value" there will be another XML inside it. like Richard and I need the "Richard" as string in the output. Please let me know still you want more information. Thanks.. Cheers, Sakthi. S

您可以使用@XmlAnyElement和DomHandler实现的组合来处理此用例。有关详细示例,请参阅:

You can use a combination of @XmlAnyElement and a DomHandler implementation to handle this use case. For a detailed example see:

  • http://bdoughan.blogspot.com/2011/04/xmlanyelement-and-non-dom-properties.html

更新#2

根据您的评论


您能告诉我什么是更改
需要在架构中完成make
提交@XmlAnyElement,因为我

构建时生成java类。

Can you please tell me what is change need to be done in the schema to make that filed "@XmlAnyElement", since I am generating the java classes on the build time.

您可以使用JAXB dom 架构注释来在属性上生成XmlAnyElement:

You can use the JAXB dom schema annotation to cause an XmlAnyElement to be generated on a property:

XJC致电

xjc -d out -b bindings.xml dom.xsd

dom.xsd

<xs:schema
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.example.com/default"
    targetNamespace="http://www.example.com/default">

    <xs:element name="customer">
        <xs:complexType>
        <xs:sequence>
            <xs:element ref="address"/>
        </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="address">
        <xs:complexType>
        <xs:attribute name="street" type="xs:string"/>
        </xs:complexType>
    </xs:element>

</xs:schema>

bindings.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    version="2.1">

    <jxb:bindings schemaLocation="string.xsd">
            <jxb:bindings node="//xs:element[@name='customer']/xs:complexType/xs:sequence/xs:element[@ref='address']">
                <jxb:dom/>
            </jxb:bindings>
    </jxb:bindings>

</jxb:bindings>

客户

Customer 上的地址属性将使用 @XmlAnyElement 进行注释:

The address property on Customer will be annotated with @XmlAnyElement:

package com.example._default;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.w3c.dom.Element;


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "address"
})
@XmlRootElement(name = "customer")
public class Customer {

    @XmlAnyElement
    protected Element address;

    public Element getAddress() {
        return address;
    }

    public void setAddress(Element value) {
        this.address = value;
    }

}






原始答案

您可能正在寻找@XmlValue注释。例如,如果您有以下类:

You may be looking for the @XmlValue annotation. For example if you had the following class:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD
public class Child {

    @XmlValue
    private String value;

}

以上课程将编组为:

<child>String Value</child>

在相应的XML模式中,子元素的类型为xs:string。

In the corresponding XML schema the type of the child element would be xs:string.

<xs:element name="child" type="xs:string"/>

这篇关于使用JAXB将子节点中的XML数据作为字符串获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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