1

I have a complex JDOM element like following (A), I need to change the structure like (B), for working on JAXB (Using with already existing classes, only thing I can do is changing the structure of xml), Can I able to do this using JDOM api?

As I am a beginer in java, it is very difficult for me, if anyone point-out a solution, it is very much helpful for me

Existing element (A)

<DETAILS>
    <ROWSET name="OPTIONS">
       <ROW num="1">
          <Uniqueno>1000</Uniqueno>
          <ROWSET name="SUSPENCE">
             <ROW num="1">
                <Uniqueno>1001</Uniqueno>
                <ROWSET name="PERSONS">
                   <ROW num="1">
                     <Name>60821894</Name>
                     <Age>44</Age>
                   </ROW>
                   <ROW num="2">
                      <Name>60821894</Name>
                      <Age>44</Age>
                   </ROW>
                </ROWSET>
                <ROWSET name="PERSONS">
                   <ROW num="1">
                     <Name>60821894</Name>
                     <Age>55</Age>
                   </ROW>
                   <ROW num="2">
                      <Name>60821894</Name>
                      <Age>55</Age>
                   </ROW>
                   <ROW num="3">
                      <Name>60821894</Name>
                      <Age>55</Age>
                   </ROW>
                </ROWSET>
             </ROW>
          </ROWSET>
       </ROW>
    </ROWSET>
 </DETAILS>

Required element (B)

<DETAILS>
    <OPTIONS>
          <Uniqueno>1000</Uniqueno>
          <SUSPENCE>
                <Uniqueno>1001</Uniqueno>
                <PERSONS>
                     <Name>60821894</Name>
                     <Age>44</Age>
                     <Name>60821894</Name>
                     <Age>44</Age>
                </PERSONS>
                <PERSONS>
                     <Name>60821894</Name>
                     <Age>55</Age>
                     <Name>60821894</Name>
                     <Age>55</Age>
                     <Name>60821894</Name>
                     <Age>55</Age>
                </PERSONS>
          </SUSPENCE>
    </OPTIONS>
 </DETAILS>
Prabhath kesav
  • 428
  • 3
  • 6
  • 21

3 Answers3

2

May I suggest to use XSLT instead. Much easier. Start with something like this

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="DETAILS/ROWSET[@name='OPTIONS']">
    <DETAILS>
        <OPTIONS>
            <xsl:apply-templates />
        </OPTIONS>
    </DETAILS>
</xsl:template>

<xsl:template match="ROW">
        <xsl:apply-templates />
</xsl:template>

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

</xsl:stylesheet>
forty-two
  • 12,204
  • 2
  • 26
  • 36
1

Looking at the xmls, these are two entirely different xmls.You need to build a xml structure similar to B dyanamically.For this, the following link will help you.

http://www.ibm.com/developerworks/java/library/j-jdom/

Hope this will help you.

UVM
  • 9,776
  • 6
  • 41
  • 66
1

You have been asking essentially the same question multiple times.

Remove XML attribute using JDOM API?

Issues in Parsing XML

If you have not yet been able to get the previous questions right, you need to take a step back and work with more basic examples before you ramp up to doing multiple element moves.

While I agree with forty-two that XSL will be a better solution in the long run, I don't think you are in a place yet where that will make things easier (for you). If you have JDOM Elements available with your data, you should figure out your Java Debugger, and inspect the Elements as you add and remove them. You need to 'play' a bit to get a better understaning of how Java, XML, and JDOM work. Right now you are asking a whole bunch of related questions that show a basic misunderstanding of what in effect are 'foundation' concepts. You need to get those foundations right before you tackle these more complex concepts.

How about you start with something simple:

XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
Document doc = new Document();
Element root = new Element("DETAILS");
doc.addContent(root);

xout.output(System.out, doc);

Element row = new Element("ROW");
root.addContent(row);

xout.output(System.out, doc);

row.detach();
xout.output(System.out, doc);

You can use the above to see how content is added, and detached from JDOM content.

Then, when you have that figured out, you can put it in loops, scans, etc. so that you can detach, and re-add content from other places in the Document hierarchy.

Community
  • 1
  • 1
rolfl
  • 17,539
  • 7
  • 42
  • 76