Search code examples
javaxmlperlantxmltask

Add/Remove/update entry between two xml file either using Ant xmltask or using perl scripting


Here is first xml file input.xml,

input.xml:

<Mapping>
        <Common>
            <Apps>
                <App ID = "id1">
                <config key = "Format" value = "PDF"/>
                </App>
                <App ID = "id2">
                <config key = "Logging" value = "no"/>
                <config key = "ExtraLogging" value = "no"/>
                </App>
            </Apps>

            <All>
            <config key="log1" value="N"/>
            </All>
        </Common>
</Mapping>

Need to read from input.xml and Add/Remove/update respective entry in output.xml.

output.xml:

    <Mapping>
            <Common>
                <Apps>
                    <App ID = "id1">
                    <config key = "Format" value = "DOC"/>
                    </App>
                    <App ID = "id2">
                    <config key = "Logging" value = "yes"/>
                    </App>
                </Apps>

                <All>
                <config key="log1" value="N"/>
                </All>
            </Common>
    </Mapping>

Solution

  • Using xmlstarlet you would write

    xmlstarlet ed \
        -u '//App[@ID="id1"]/config[@key="Format"]/@value' -v "DOC" \
        -u '//App[@ID="id2"]/config[@key="Logging"]/@value' -v "yes" \
        -d '//App[@ID="id2"]/config[@key="ExtraLogging"]' \
            input.xml > output.xml