根据属性合并XML - 更改值,添加或删除标记

时间:2015-10-20 10:07:14

标签: xml xml-parsing

我有一个要求,根据用户提供的xml,我必须修改我的xml并添加,删除或修改现有的节点值。有谁能建议我如何实现它?

实施例: master.xml

    <?xml version="1.0" encoding="UTF-8"?>
<ds>
    <screen id="screenVerification">
        <tasks id="verificationTasks">
            <task id="verificationTask"  server="database">
                <class id="verificationClass" mode="java" value="verificationResults">classA</class>
            </task>

            <task id="verification01Task">
                <class id="verificationClass" mode="java" value="verificationResults">classB</class>
            </task>

            <task id="verification02Task">
                <class id="verificationClass" mode="java" value="verificationResults">classC</class>
            </task>
        </tasks>
    </screen>   
    <screen id="screenMigration">
        <tasks id="migrationTasks">
            <task id="migrationTask">
                <ant id="dbMigrateAnt" mode="ant" value="migrationResults">
                    <file>ops.xml</file>
                    <target>dbMigrate</target>
                </ant>
            </task>
        </tasks>
    </screen>
</ds>

user.xml

<?xml version="1.0" encoding="UTF-8"?>
    <ds>
        <screen id="screenVerification">
            <tasks id="verificationTasks">
                <task id="verificationTask"  server="database">
                    <class id="verificationClass" mode="java" value="verificationResults">classX</class>
                </task>
                <task id="verification01Task"  server="database">
                    <ant id="dbMigrateAnt" mode="ant" value="migrationResults">
                        <file>a.xml</file>
                        <target>mig</target>
                    </ant>
                </task>
            </tasks>
        </screen>   
        <screen id="screenMigration">
            <tasks id="migrationTasks">
                <task id="migrationTask">
                </task>
                <task id="addedtask">
                    <cmd id="sql" mode="sql">select * from users;</cmd>
                </task>
            </tasks>
        </screen>
    </ds>

为result.xml (需要 1.将具有id = verificationClass的class的值替换为classX 2.需要使用id = verification01Task更改任务 3.需要使用id = migrationTask删除任务 4.需要添加id = addedtask

的任务
<?xml version="1.0" encoding="UTF-8"?>
<ds>
    <screen id="screenVerification">
        <tasks id="verificationTasks">
            <task id="verificationTask"  server="database">
                <class id="verificationClass" mode="java" value="verificationResults">classX</class>
            </task>         
            <task id="verification01Task"  server="database">
                <ant id="dbMigrateAnt" mode="ant" value="migrationResults">
                    <file>a.xml</file>
                    <target>mig</target>
                </ant>
            </task>         
            <task id="verification02Task">
                <class id="verificationClass" mode="java" value="verificationResults">classC</class>
            </task>
        </tasks>
    </screen>   
    <screen id="screenMigration">
        <tasks id="migrationTasks">
            <task id="addedtask">
                <cmd id="sql" mode="sql">select * from users;</cmd>
            </task>
        </tasks>
    </screen>
</ds>

0 个答案:

没有答案