0

I want to sort un-ordered XML files so that two files can be comparable. XML file should be sorted based on first outermost tag and then inner tags. I want some generic code. For Example:

My XML file is :

<xn:rootElement>
    <subs:Parent id="2">
        <subs:Child1 id="2">
            <attributes>
                <attribute1></attribute1>
                <attribute2></attribute2>
            <attributes>
        </subs:Child1>

    </subs:Parent>
    <subs:Parent id="2">
        <subs:Child1 id="1">
            <attributes>
                <attribute1></attribute1>
                <attribute2></attribute2>
            <attributes>
        </subs:Child1>
    </subs:Parent>
</xn:rootElement>

I want the following result, as parent id is same it is sorted on base of child element.

<xn:rootElement>
    <subs:Parent id="2">
        <subs:Child1 id="1">
            <attributes>
                <attribute1></attribute1>
                <attribute2></attribute2>
            <attributes>
        </subs:Child1>

    </subs:Parent>
    <subs:Parent id="2">
        <subs:Child1 id="2">
            <attributes>
                <attribute1></attribute1>
                <attribute2></attribute2>
            <attributes>
        </subs:Child1>
    </subs:Parent>
</xn:rootElement>

Need to check every possibility , if parenet id is same then sort on base of child id.

Note: Size of XMLs is very Large.

Thanks a lot in advance.

7
  • what have you done so far? Commented Jul 29, 2015 at 7:30
  • what is your question? Commented Jul 29, 2015 at 7:34
  • I tried with XSLT but in that case i need to specify the tag with which i want to sort in style-sheet. I tried with SAXBuilder but i am not satisfied because it is not even generic code. I want some generic code. Commented Jul 29, 2015 at 7:35
  • Convert your XML to Java Objects using JAXB or any other method you find it suitable, override equals and hashCode and compare Objects. Comparing XMLs as a String is not a good approach. Commented Jul 29, 2015 at 7:41
  • how large is your xml. You said that you tried using xslt. did xslt transformation succeeded without OutOfMemory error. Commented Jul 29, 2015 at 7:44

1 Answer 1

1

I don't have code ready to share. but Pseudocode is:

  1. Try creating objects with Parent and having list of childs.
  2. Sort the list of parents and its respective childs.
  3. Create a XML again.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.