我结合使用 xmlstreamwriter 和 jaxb 来编组一个大的 xml 文件。我正在使用 jaxb 创建子树,但问题是当我编组子树时,它的前缀为默认命名空间,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<MessageModel xmlns="urn:schemas.mycompany.com/ENT/MessageModel/2013/09/19">
<MessageHeader xmlns="" xmlns:ns2="urn:schemas.mycompany.com/ENT/MessageModel/2013/09/19">
<ns2:ProviderID>5922</ns2:ProviderID>
<ns2:EffectiveDT>2016-08-08-04:00</ns2:EffectiveDT>
<ns2:PartyCount>0</ns2:PartyCount>
<ns2:ArrangementCount>1</ns2:ArrangementCount>
<ns2:AppMetaDataString>ter</ns2:AppMetaDataString>
</MessageHeader>
</MessageModel>
我正在使用以下代码进行编组:
StringWriter result = new StringWriter();
MessageHeaderType messageHeaderType = createMessageHeader(objectFactory);
JAXBElement<MessageHeaderType> element = new JAXBElement<MessageHeaderType>(new QName("MessageHeader"), MessageHeaderType.class, messageHeaderType);
XMLStreamWriter xmlOut = XMLOutputFactory.newFactory().createXMLStreamWriter(result);
**//Setting default namespace**
xmlOut.setDefaultNamespace("urn:schemas.mycompany.com/ENT/MessageModel/2013/09/19");
xmlOut.writeStartDocument();
xmlOut.writeStartElement("urn:schemas.mycompany.com/ENT/MessageModel/2013/09/19", "MessageModel");
xmlOut.writeNamespace("", "urn:schemas.mycompany.com/ENT/MessageModel/2013/09/19");
JAXBContext context = JAXBContext.newInstance(MessageHeaderType.class);
Marshaller marshaller = context.createMarshaller();
//marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.marshal(element, xmlOut);
xmlOut.writeEndDocument();
xmlOut.close();
System.out.println(result.toString());
我正在设置默认命名空间,但它仍然使用命名空间创建子树。我可以做些什么来生成带有 jaxb 但没有命名空间的子树?