我坐在这里与我经验很少的JAXB(我真的是.NET专家)。但是我想为客户提供从.NET到groovy的系统。
无论如何。这是问题。我有一个巨大的xml文件,在旧系统中,每个xml标记上都有xml名称空间,如下所示:
<RapportV4Type xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:oio:ebst:diadem:4">
<RapportResume xmlns="urn:oio:ebst:diadem:resume:1">
<RapportvalgmulighedSamling>
<Rapportvalgmulighed>
<RapportResumeTekst></RapportResumeTekst>
<RapportvalgmulighedIdentifikator xmlns="urn:oio:ebst:diadem:1"></RapportvalgmulighedIdentifikator>
</Rapportvalgmulighed>
<Rapportvalgmulighed>
<RapportResumeTekst></RapportResumeTekst>
<RapportvalgmulighedIdentifikator xmlns="urn:oio:ebst:diadem:1"></RapportvalgmulighedIdentifikator>
</Rapportvalgmulighed>
<Rapportvalgmulighed>
<RapportResumeTekst></RapportResumeTekst>
<RapportvalgmulighedIdentifikator xmlns="urn:oio:ebst:diadem:1"></RapportvalgmulighedIdentifikator>
</Rapportvalgmulighed>
<Rapportvalgmulighed>
<RapportResumeTekst></RapportResumeTekst>
<RapportvalgmulighedIdentifikator xmlns="urn:oio:ebst:diadem:1"></RapportvalgmulighedIdentifikator>
</Rapportvalgmulighed>
</RapportvalgmulighedSamling>
</RapportResume>
<RapportstatusV2 xmlns="urn:oio:ebst:diadem:2">
<RapportstatusV2Kode></RapportstatusV2Kode>
</RapportstatusV2>
<GennemfoertDato xmlns="urn:oio:ebst:diadem:1"></GennemfoertDato>
<GenbestillingerTilbageKvantitet xmlns="urn:oio:ebst:diadem:1"></GenbestillingerTilbageKvantitet>
<GenbestillingsperiodesOphoerDato xmlns="urn:oio:ebst:diadem:1"></GenbestillingsperiodesOphoerDato>
<RapportbestillingV3 xmlns="urn:oio:ebst:diadem:3">
<RapportBestillingIdentifikator xmlns="urn:oio:ebst:diadem:1"></RapportBestillingIdentifikator>
<RapportbestillingDatoV2 xmlns="urn:oio:ebst:diadem:2"></RapportbestillingDatoV2>
<EjendomIdentifikator xmlns="urn:oio:ebst:diadem:1"></EjendomIdentifikator>
<KommuneIdentifikator xmlns="urn:oio:ebst:diadem:1"></KommuneIdentifikator>
<RapportvalgmulighedValgSamling xmlns="urn:oio:ebst:diadem:1">
<RapportvalgmulighedValgIdentifikator></RapportvalgmulighedValgIdentifikator>
<RapportvalgmulighedValgIdentifikator></RapportvalgmulighedValgIdentifikator>
<RapportvalgmulighedValgIdentifikator></RapportvalgmulighedValgIdentifikator>
<RapportvalgmulighedValgIdentifikator></RapportvalgmulighedValgIdentifikator>
</RapportvalgmulighedValgSamling>
<AdviseringsAdresse xmlns="urn:oio:ebst:diadem:1">
<EmailAdresseIdentifikator></EmailAdresseIdentifikator>
<TelefonNummerIdentifikator></TelefonNummerIdentifikator>
</AdviseringsAdresse>
<FuldmagtIndhentetIndikator xmlns="urn:oio:ebst:diadem:1"></FuldmagtIndhentetIndikator>
<RapportFortroligIndikator xmlns="urn:oio:ebst:diadem:1"></RapportFortroligIndikator>
</RapportbestillingV3>
问题在于使用以下代码:
static <T> Element serializeGenericObjectToXmlTypeElement(T object, Class<T> genericType, boolean removeDeclaration = false) {
JAXBContext jc = JAXBContext.newInstance(genericType)
Marshaller marshaller = jc.createMarshaller()
if (removeDeclaration) {
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true)
}
Writer writer = new StringWriter()
marshaller.marshal(object, writer)
def xmlString = writer.toString()
def stringReader = new StringReader(xmlString)
def builder = new SAXBuilder()
def doc = builder.build(stringReader)
def elem = doc.getRootElement()
return elem
}
将名称空间移至根元素,然后每个xml标记都获得一个前缀。像这样:
<RapportV4Type xmlns="urn:oio:ebst:diadem:4" xmlns:ns6="urn:oio:ebst:diadem:metadata:1" xmlns:ns5="urn:oio:ebst:diadem:1" xmlns:ns7="urn:oio:ebst:diadem:metadata:2" xmlns:ns2="urn:oio:ebst:diadem:2" xmlns:ns4="urn:oio:ebst:diadem:resume:1" xmlns:ns3="urn:oio:ebst:diadem:3">
<ns4:RapportResume>
<ns4:RapportvalgmulighedSamling>
<ns4:Rapportvalgmulighed>
<ns4:RapportResumeTekst></ns4:RapportResumeTekst>
<ns5:RapportvalgmulighedIdentifikator></ns5:RapportvalgmulighedIdentifikator>
</ns4:Rapportvalgmulighed>
<ns4:Rapportvalgmulighed>
<ns4:RapportResumeTekst></ns4:RapportResumeTekst>
<ns5:RapportvalgmulighedIdentifikator></ns5:RapportvalgmulighedIdentifikator>
</ns4:Rapportvalgmulighed>
<ns4:Rapportvalgmulighed>
<ns4:RapportResumeTekst></ns4:RapportResumeTekst>
<ns5:RapportvalgmulighedIdentifikator></ns5:RapportvalgmulighedIdentifikator>
</ns4:Rapportvalgmulighed>
<ns4:Rapportvalgmulighed>
<ns4:RapportResumeTekst></ns4:RapportResumeTekst>
<ns5:RapportvalgmulighedIdentifikator></ns5:RapportvalgmulighedIdentifikator>
</ns4:Rapportvalgmulighed>
</ns4:RapportvalgmulighedSamling>
我的所有类都是从XSD文件生成的(需要序列化和反序列化的类)。在.NET解决方案中与文件配合使用时效果很好。但是有了JAXB,它就提供了这种结构。
我试图在每个类中用名称空间声明根元素,但这没有任何作用。我还从这里尝试了接受的答案:JAXB marshalling XMPP stanzas但这也不起作用。
有人有什么建议吗?