简体   繁体   中英

XML remove all child node from a node and create new one

I have this xml file:

<achievements>
        <achievement id="1" name="Monster Slayer Lv1" description="Slay 15 monsters of any type" icon="Icon.skill0496" categoryId="1">
            <conditions>
                <condition name="Level" val="50" />
            </conditions>
            <items>
                <item id="6393" count="1" />
            </items>
        </achievement>  
        <achievement id="2" name="Monster Slayer Lv2" description="Slay 50 monsters of any type" icon="Icon.skill0497" categoryId="1">
            <conditions>
                <condition name="MonsterKill" val="50" />
            </conditions>
            <items>
                <item id="57" count="50000000" />
            </items>
        </achievement>
    </achievements>

And i want my code:

try
        {
            final File inputFile = new File("C:/Users/ProjectX/Desktop/achievement.xml");
            final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            final Document doc = dBuilder.parse(inputFile);

            for (Node node = doc.getFirstChild().getFirstChild(); node != null; node = node.getNextSibling())
            {
                if (node.getNodeName().equalsIgnoreCase("achievements"))
                {
                    for (Node ach_node = node.getFirstChild().getFirstChild(); ach_node != null; ach_node = ach_node.getNextSibling())
                    {
                        if (ach_node.getNodeName().equalsIgnoreCase("achievement"))
                        {

                        }
                    }
                }
            }
        }
        catch (final Exception e)
        {
            e.printStackTrace();
        }

to read until achievement and delete all sub-node (which include all condition and items or any other child node it might have). In addition i want to replace with my own node such as"

<dropList id="x">
     <itemId="x" min="x" max="x" chance="x" />
</dropList>

Anyone know how to do this? Thanks in advance a lot to all community member who spend their time reading this.

<achievements>
        <achievement id="1" name="Monster Slayer Lv1" description="Slay 15 monsters of any type" icon="Icon.skill0496" categoryId="1">
            <conditions>
                <condition name="Level" val="50" />
            </conditions>
            <dropList id="1">
               <itemId="x" min="x" max="x" chance="x" />
             </dropList>
            <dropList id="2">
               <itemId="x" min="x" max="x" chance="x" />
            </dropList>
        </achievement>  
        <achievement id="2" name="Monster Slayer Lv2" description="Slay 50 monsters of any type" icon="Icon.skill0497" categoryId="1">
            <conditions>
                <condition name="MonsterKill" val="50" />
            </conditions>
            <dropList id="1">
               <itemId="x" min="x" max="x" chance="x" />
             </dropList>
            <dropList id="2">
               <itemId="x" min="x" max="x" chance="x" />
            </dropList>
        </achievement>
    </achievements>

It is better to use an XSLT transformation for your case. It has a specific pattern for exactly what you need. It is called Identity Transform .

All you need to do is just to run the XSLT below in your Java code.

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="items">
        <dropList id="1">
            <item Id="x" min="x" max="x" chance="x"/>
        </dropList>
        <dropList id="2">
            <item Id="x" min="x" max="x" chance="x"/>
        </dropList>
    </xsl:template>
</xsl:stylesheet>

Output

<achievements>
  <achievement id="1" name="Monster Slayer Lv1" description="Slay 15 monsters of any type" icon="Icon.skill0496" categoryId="1">
    <conditions>
      <condition name="Level" val="50"/>
    </conditions>
    <dropList id="1">
      <item Id="x" min="x" max="x" chance="x"/>
    </dropList>
    <dropList id="2">
      <item Id="x" min="x" max="x" chance="x"/>
    </dropList>
  </achievement>
  <achievement id="2" name="Monster Slayer Lv2" description="Slay 50 monsters of any type" icon="Icon.skill0497" categoryId="1">
    <conditions>
      <condition name="MonsterKill" val="50"/>
    </conditions>
    <dropList id="1">
      <item Id="x" min="x" max="x" chance="x"/>
    </dropList>
    <dropList id="2">
      <item Id="x" min="x" max="x" chance="x"/>
    </dropList>
  </achievement>
</achievements>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM