Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过jaxb从生成的XML中删除不需要的元素_Java_Xml_Jaxb_Marshalling_Unmarshalling - Fatal编程技术网

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

Java 如何通过jaxb从生成的XML中删除不需要的元素,java,xml,jaxb,marshalling,unmarshalling,Java,Xml,Jaxb,Marshalling,Unmarshalling,我想知道是否有任何方法可以使用jaxb从生成的xml中删除不需要的元素 <xsd:element name="Title" maxOccurs="1" minOccurs="0"> <xsd:annotation> <xsd:documentation> A name given to the digital recor

我想知道是否有任何方法可以使用jaxb从生成的xml中删除不需要的元素

           <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>
编组代码

            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值设置为空字符串
,它将生成
,如果将其设置为
null
,它将完全忽略元素。

我正在利用XmlAdapter,实现一个透明适配器,如果集合或字符串为空,它将在封送时返回null值

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;
    }
}


在这里发布您尝试过的源代码。@THANGA我已经更新了这个问题。有没有办法让JAXB生成空字符串?看起来很少有解析器将其视为完全相同的内容。@Teddy我不知道,而且由于这两种形式都是相同的XML,这在“少数解析器”中是一个错误,而不是JAXB中的一个问题。如果封送到XMLStreamWriter并使用将其作为配置选项提供的StAX实现,则可以解决此问题。我知道Woodstox有可能解决这个问题,如果JAXB对编写器进行单独的开始/结束标记调用,而不是调用
writeEmptyElement
,那么您必须自己进行实验,看看什么是有效的。
  private JAXBContext jaxbContext;
    private Unmarshaller unmarshaller;
    private SchemaFactory factory;
    private Schema schema;
    private Marshaller marshaller;
            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);
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;
    }
}
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=StringAdapter.class, type=String.class)
})
your.package