简体   繁体   English

如何通过 jaxb 从生成的 XML 中删除不需要的元素

[英]How to remove not required Elements from generated XML via jaxb

I want to know if there is anyway for removing not required elements from generated xml using jaxb.I have my xsd element definition as follows.我想知道是否可以使用 jaxb 从生成的 xml 中删除不需要的元素。我的 xsd 元素定义如下。

           <xsd:element name="Title" maxOccurs="1" minOccurs="0">
                <xsd:annotation>
                    <xsd:documentation>
                        A name given to the digital record.
                    </xsd:documentation>
                </xsd:annotation>
                <xsd:simpleType>
                    <xsd:restriction base="xsd:string">
                        <xsd:minLength value="1"></xsd:minLength>
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:element>

As you can see it is not a mandatory element because如您所见,它不是强制性元素,因为

minOccurs="0" minOccurs="0"

But if it is not empty the length should be 1.但如果它不为空,则长度应为 1。

<xsd:minLength value="1"></xsd:minLength>

At the time of marshalling if I left the Title field blank it is throwing the SAXException because of min-length restriction.在编组时,如果我将 Title 字段留空,由于最小长度限制,它会抛出SAXException So what I want to do is to remove the whole occurrence of <Title/> from generated XML.Right now i have removed the min-length restriction so it is adding the <Title> element as EMPTY所以我想要做的是从生成的 XML 中删除<Title/>的整个出现。现在我已经删除了最小长度限制,因此它将<Title>元素添加为EMPTY

<Title></Title>

But I do not want it like this.Any help is appreciated.I am using jaxb 2.0 for Marshalling.但我不想要这样。感谢任何帮助。我正在使用 jaxb 2.0 进行编组。

UPDATE:更新:

Following is my variable definiton :以下是我的变量定义:

  private JAXBContext jaxbContext;
    private Unmarshaller unmarshaller;
    private SchemaFactory factory;
    private Schema schema;
    private Marshaller marshaller;

Marshalling code.编组代码。

            jaxbContext = JAXBContext.newInstance(ERecordType.class);
            marshaller = jaxbContext.createMarshaller();
            factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schema = factory.newSchema((new File(xsdLocation)));
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            ERecordType e = new ERecordType();
            e.setCataloging(rc);
            /**
             * Validate Against Schema.
             */
            marshaller.setSchema(schema);
            /**
             * Marshal will throw an exception if XML not validated against
             * schema.
             */
            marshaller.marshal(e, System.out);

如果您将 title 值设置为空字符串""它会生成<Title/> ,如果您将它设置为null它应该完全省略该元素。

I'm exploiting the XmlAdapter, implementing a transparent adapter which return in marshaling the null value if the Collection or String is empty.我正在利用 XmlAdapter,实现一个透明的适配器,如果 Collection 或 String 为空,它会在封送空值时返回。

public class XMLStringAdapter extends XmlAdapter<String, String> {


    @Override
    public String unmarshal(String v) throws Exception {
        return v;
    }

    @Override
    public String marshal(String v) throws Exception {
        return v != null && v.isEmpty() ? null : v;
    }
}

Also add in package-info.java还要添加 package-info.java

@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
})
your.package

https://www.eclipse.org/eclipselink/documentation/2.6/moxy/advanced_concepts006.htm https://www.eclipse.org/eclipselink/documentation/2.6/moxy/advanced_concepts006.htm

How can JAXB XmlAdapter be used to marshall lists? 如何使用 JAXB XmlAdapter 来编组列表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM