Java Джексон - сериализация и десериализация XML строки с сохраненными пространствами имен? - PullRequest
0 голосов
/ 04 мая 2020

У меня есть эта длинная XML строка с кратными XML пространствами имен (это сгенерировано из других источников), и пространства имен случайно создаются авторами.

Я использую эту зависимость Maven:

    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.8.7</version>
    </dependency>

Задача: прочитать это XML для объекта POJO и добавить некоторые другие XML элементы и сериализовать объект POJO в новый XML строка . Исходный код XML выглядит следующим образом:

<lcrmd:LandCoverGridCoverageMD
                        xmlns:ad="http://inspire.ec.europa.eu/schemas/ad/4.0"
                        xmlns:base="http://inspire.ec.europa.eu/schemas/base/3.3"
                        xmlns:base2="http://inspire.ec.europa.eu/schemas/base2/2.0"
                        xmlns:gco="http://www.isotc211.org/2005/gco"
                        xmlns:gmd="http://www.isotc211.org/2005/gmd"
                        xmlns:gn="http://inspire.ec.europa.eu/schemas/gn/4.0"
                        xmlns:lcn="http://inspire.ec.europa.eu/schemas/lcn/4.0"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                        xmlns:lcrmd="http://inspire.ec.europa.eu/schemas/lcrmd/4.0" xsi:schemaLocation="http://inspire.ec.europa.eu/schemas/lcrmd/4.0 http://test.datacove.eu/LandCoverRasterMDExt.xsd">
                        <lcrmd:inspireId>
                            <base:Identifier>
                                <base:localId>CLC Test</base:localId>
                                <base:namespace>Datacove.eu</base:namespace>
                            </base:Identifier>
                        </lcrmd:inspireId>
                        <lcrmd:beginLifespanVersion>2018-07-18T00:00:00</lcrmd:beginLifespanVersion>
                        <lcrmd:extent>
                            <gmd:EX_Extent>
                                <gmd:geographicElement>
                                <gmd:EX_GeographicBoundingBox>
                                <gmd:westBoundLongitude>
                                <gco:Decimal>1.0</gco:Decimal>
                                </gmd:westBoundLongitude>
                                <gmd:eastBoundLongitude>
                                <gco:Decimal>10.0</gco:Decimal>
                                </gmd:eastBoundLongitude>
                                <gmd:southBoundLatitude>
                                <gco:Decimal>1.0</gco:Decimal>
                                </gmd:southBoundLatitude>
                                <gmd:northBoundLatitude>
                                <gco:Decimal>3.0</gco:Decimal>
                                </gmd:northBoundLatitude>
                                </gmd:EX_GeographicBoundingBox>
                                </gmd:geographicElement>
                            </gmd:EX_Extent>
                        </lcrmd:extent>
                        <lcrmd:name>Corine Land Cover Coverage Test</lcrmd:name>
                        <lcrmd:nomenclatureDocumentation>
                            <lcn:LandCoverNomenclature>
                                <lcn:inspireId>
                                <base:Identifier>
                                <base:localId>CLC Nom Test</base:localId>
                                <base:namespace>Datacove.eu</base:namespace>
                                </base:Identifier>
                                </lcn:inspireId>
                                <lcn:nomenclatureCodeList/>
                                <lcn:responsibleParty>
                                <base2:RelatedParty>
                                <!-- individual responsible for the vessel -->
                                <base2:individualName>
                                <gmd:LocalisedCharacterString>Jane Doe</gmd:LocalisedCharacterString>
                                </base2:individualName>
                                <!-- organization responsible for the nomenclature  -->
                                <base2:organisationName>
                                <gmd:LocalisedCharacterString>Corine Unlimited</gmd:LocalisedCharacterString>
                                </base2:organisationName>
                                <base2:contact>
                                <base2:Contact>
                                <base2:address>
                                <ad:AddressRepresentation>
                                <ad:adminUnit>
                                <gn:GeographicalName>
                                <gn:language>eng</gn:language>
                                <!-- language of municipality name-->

                                <gn:nativeness
                                nilReason="missing" xsi:nil="true"/>

                                <gn:nameStatus
                                nilReason="missing" xsi:nil="true"/>

                                <gn:sourceOfName
                                nilReason="missing" xsi:nil="true"/>

                                <gn:pronunciation
                                nilReason="missing" xsi:nil="true"/>
                                <gn:spelling>
                                <gn:SpellingOfName>
                                <!-- municipality name -->
                                <gn:text>Corine</gn:text>
                                <gn:script>Latn</gn:script>
                                </gn:SpellingOfName>
                                </gn:spelling>
                                </gn:GeographicalName>
                                </ad:adminUnit>
                                <!-- Street address -->
                                <ad:locatorDesignator>Seaside Lane 1</ad:locatorDesignator>
                                <!-- Postal Code-->
                                <ad:postCode>OC-1234</ad:postCode>
                                </ad:AddressRepresentation>
                                </base2:address>
                                <!-- e-mail of responsible person -->
                                <base2:electronicMailAddress>jane.doe@Corine.eu</base2:electronicMailAddress>
                                <!-- phonel of responsible person -->
                                <base2:telephoneVoice>+12 3 456 789</base2:telephoneVoice>
                                <!-- web URI of responsible organization-->
                                <base2:website>http://www.Corine.eu</base2:website>
                                </base2:Contact>
                                </base2:contact>
                                </base2:RelatedParty>
                                </lcn:responsibleParty>
                            </lcn:LandCoverNomenclature>
                        </lcrmd:nomenclatureDocumentation>
                        <lcrmd:validFrom>2018-07-18</lcrmd:validFrom>
                        <lcrmd:validTo>2020-07-18</lcrmd:validTo>
                    </lcrmd:LandCoverGridCoverageMD>

Пока что я сделал, чтобы отобразить эту вложенную строку XML со random child XML elements в Объект POJO с:

public class CoverageMetadata {

   @JsonAnyGetter
   public Map<String, Object> getGlobalAttributesMap() {
       return this.additionalProperties;
   }

   @JsonAnySetter
   public void setAdditionalProperty(String name, Object value) {
      this.additionalProperties.put(name, value);
   }

}

и кодом для десериализации строки XML для этого объекта:

XmlMapper xmlMapper = new XmlMapper();
xmlMapper.readValue(xmlString, CoverageMetadata.class);

Когда я пытаюсь сериализовать xmlMapper в XML, Я получил результат без пространств имен для XML элементов

    <LandCoverGridCoverageMD>
    <schemaLocation>
http://inspire.ec.europa.eu/schemas/lcrmd/4.0 http://test.datacove.eu/LandCoverRasterMDExt.xsd
</schemaLocation>
    <inspireId>
        <Identifier>
            <localId>CLC Test</localId>
            <namespace>Datacove.eu</namespace>
        </Identifier>
    </inspireId>
    <beginLifespanVersion>2018-07-18T00:00:00</beginLifespanVersion>
    <extent>
        <EX_Extent>
            <geographicElement>
                <EX_GeographicBoundingBox>
                    <westBoundLongitude>
                        <Decimal>1.0</Decimal>
                    </westBoundLongitude>
                    <eastBoundLongitude>
                        <Decimal>10.0</Decimal>
                    </eastBoundLongitude>
                    <southBoundLatitude>
                        <Decimal>1.0</Decimal>
                    </southBoundLatitude>
                    <northBoundLatitude>
                        <Decimal>3.0</Decimal>
                    </northBoundLatitude>
                </EX_GeographicBoundingBox>
            </geographicElement>
        </EX_Extent>
    </extent>
    <name>Corine Land Cover Coverage Test</name>
    <nomenclatureDocumentation>
        <LandCoverNomenclature>
            <inspireId>
                <Identifier>
                    <localId>CLC Nom Test</localId>
                    <namespace>Datacove.eu</namespace>
                </Identifier>
            </inspireId>
            <nomenclatureCodeList />
            <responsibleParty>
                <RelatedParty>
                    <individualName>
                        <LocalisedCharacterString>Jane Doe</LocalisedCharacterString>
                    </individualName>
                    <organisationName>
                        <LocalisedCharacterString>Corine Unlimited</LocalisedCharacterString>
                    </organisationName>
                    <contact>
                        <Contact>
                            <address>
                                <AddressRepresentation>
                                    <adminUnit>
                                        <GeographicalName>
                                            <language>eng</language>
                                            <nativeness>
                                                <nilReason>missing</nilReason>
                                                <nil>true</nil>
                                            </nativeness>
                                            <nameStatus>
                                                <nilReason>missing</nilReason>
                                                <nil>true</nil>
                                            </nameStatus>
                                            <sourceOfName>
                                                <nilReason>missing</nilReason>
                                                <nil>true</nil>
                                            </sourceOfName>
                                            <pronunciation>
                                                <nilReason>missing</nilReason>
                                                <nil>true</nil>
                                            </pronunciation>
                                            <spelling>
                                                <SpellingOfName>
                                                    <text>Corine</text>
                                                    <script>Latn</script>
                                                </SpellingOfName>
                                            </spelling>
                                        </GeographicalName>
                                    </adminUnit>
                                    <locatorDesignator>Seaside Lane 1</locatorDesignator>
                                    <postCode>OC-1234</postCode>
                                </AddressRepresentation>
                            </address>
                            <electronicMailAddress>jane.doe@Corine.eu</electronicMailAddress>
                            <telephoneVoice>+12 3 456 789</telephoneVoice>
                            <website>http://www.Corine.eu</website>
                        </Contact>
                    </contact>
                </RelatedParty>
            </responsibleParty>
        </LandCoverNomenclature>
    </nomenclatureDocumentation>
    <validFrom>2018-07-18</validFrom>
    <validTo>2020-07-18</validTo>
</LandCoverGridCoverageMD>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...