1

我有这个xml:

<tag1 xmlns="myNamespace.com/#">
    <insideTag myAttribute="5">
        some text
    </insideTag>
</tag1>

但编组后它变成了这样:

<tag1 xmlns="myNamespace.com">
    <insideTag xmlns:ns1="myNamespace.com/#" xmlns="" myAttribute="5">
        some text
    </insideTag>
</tag1>

我知道,它几乎相同,但我真的需要 xml 像第一个一样。

我没有这个xml的xsd。实际上,该 xml 被解析为 org.w3c.dom.Element,添加到 ObjectType(下面的 xsd 和类简化实现)中,然后编组此 ObjectType。
如果这很重要,我正在使用 NamespacePrefixMapper。xsd(请注意,attributeFormDefault 未设置)

<schema xmlns="http://www.w3.org/2001/XMLSchema" version="0.1"
  xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
  targetNamespace="http://www.w3.org/2000/09/xmldsig#"
  elementFormDefault="qualified">

    <complexType name="ObjectType" mixed="true">
        <sequence minOccurs="0" maxOccurs="unbounded">
            <any namespace="##any" processContents="lax"/>
        </sequence>
    </complexType>
</schema>

班级:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ObjectType", namespace = "http://www.w3.org/2000/09/xmldsig#",
   propOrder = { "content" })
public class ObjectType{
    @XmlMixed
    @XmlAnyElement(lax = true)
    protected List<Object> content;

    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }
}

所以...有人知道如何解决这个问题吗?
顺便说一句......该解决方案也必须与 jaxb-impl-2.1.4 一起使用。

谢谢各位的帮助。

4

1 回答 1

0

为了避免不必要的命名空间,您需要ObjectFactory像这样在类中注册:

@XmlElementDecl(name="CurrencyPayTypeCode") 
public JAXBElement<OLILUMEASUNITS> createCurrencyPayTypeCode(OLILUMEASUNITS elem) {
                return new JAXBElement<OLILUMEASUNITS>(new QName("OLILUMEASUNITS"), OLILUMEASUNITS.class, elem); 
}

在哪里:

  1. CurrencyPayTypeCode- insideTag 的名称
  2. OLILUMEASUNITS- 一个可以解组这个标签的类
于 2014-11-06T07:42:54.890 回答