Пространство имен на каждом поле JAXB
Я сижу здесь с 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 маршалинг XMPP-строф Но это тоже не сработало.
У кого-нибудь есть предложения?