简体   繁体   English

使用DOM解析器对XML中的节点进行排序

[英]Sort nodes in XML using DOM parser

How can I sort the XML nodes according to the tag and append in the new XML using DOM parser or can it be done using DOM parser. 如何根据标记对XML节点进行排序,并使用DOM解析器将其附加到新XML中,或者可以使用DOM解析器完成。 We've used DOM parser extensively for appending nodes into a new file but I am not able to sort the nodes. 我们已经广泛使用DOM解析器将节点追加到新文件中,但是我无法对节点进行排序。

Any help would be highly appreciated. 任何帮助将不胜感激。

Input.xml Input.xml

<rss version="2.0">
    <Configs>
        <Value>defaultValue</Value>
        <Config name="test1">
            <title>Title 1</title>
            <author>Author1</author>
            <value>5600</value>
            <order>02</order>
        </Config>
        <Config name="test2">
            <title>Title 2</title>
            <author>Author2</author>
            <Value>6100</Value>
            <order>01</order>
        </Config>
    </Configs>
    <Ratings>
        <body>
            <Items name="ac_object1">
                <something1>something1</something1>
                <value>someValue1</value>
                <order>02</order>
            </Items>
            <Items name="op_object2">
                <something1>something2</something1>
                <value>someValue2</value>
                <order>03</order>
            </Items>
            <Items name="vt_object3">
                <something1>something3</something1>
                <value>someValue3</value>
                <order>01</order>
            </Items>
        </body>
    </Ratings>
</rss>

Expected Output.xml 预期的Output.xml

<rss version="2.0">
    <Configs>
        <Value>defaultValue</Value>
        <Config name="test2">
            <title>Title 2</title>
            <author>Author2</author>
            <Value>6100</Value>
            <order>01</order>
        </Config>
        <Config name="test1">
            <title>Title 1</title>
            <author>Author1</author>
            <value>5600</value>
            <order>02</order>
        </Config>
    </Configs>
    <Ratings>
        <body>
            <Items name="vt_object3">
                <something1>something3</something1>
                <value>someValue3</value>
                <order>01</order>
            </Items>
            <Items name="ac_object1">
                <something1>something1</something1>
                <value>someValue1</value>
                <order>02</order>
            </Items>
            <Items name="op_object2">
                <something1>something2</something1>
                <value>someValue2</value>
                <order>03</order>
            </Items>
        </body>
    </Ratings>
</rss>

You really don't want to do this using low-level DOM interfaces. 您确实不想使用低级DOM接口来执行此操作。 Here's how to do it in XSLT 3.0 (which you can call from Java after installing Saxon-HE): 这是在XSLT 3.0(在安装Saxon-HE之后可以从Java调用)中执行此操作的方法:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform> version="3.0">
    <xsl:mode on-no-match="shallow-copy"/>
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="*[*/order]">
        <xsl:copy>
            <xsl:apply-templates>
                <xsl:sort select="number(order)"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

With a few extra lines of code you could also do it using XSLT 1.0, which comes bundled with the JDK. 使用几行额外的代码,您还可以使用JDK附带的XSLT 1.0。

How it works: 这个怎么运作:

  • The xsl:mode declaration says that the default action for elements is to copy the element and then process its children xsl:mode声明表示元素的默认操作是复制元素,然后处理其子元素

  • xsl:strip-space says ignore whitespace in the input xsl:strip-space表示忽略输入中的空格

  • xsl:output says add indentation in the output xsl:output表示在输出中添加缩进

  • The xsl:template rule says that when processing an element that has order elements among its grandchildren, copy the start and end tag, and process the children in sorted order of the numeric value of their order child element. xsl:template规则表示,在处理其子孙中具有order元素的元素时,请复制start和end标记,并以其order子元素的数值的排序顺序处理子元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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