0

I have a JDOM element like the following

Example:XML(JDOM Element)

<Details>
<Uniqueno>11111</Uniqueno>
<ROWSET name="Persons">
<ROW num="1">
<Name>60821894</Name>
<Age>938338789</Age>
</ROW>
<ROW num="2">
<Name>60821894</Name>
<Age>938338789</Age>
</ROW>
</ROWSET>
</Details>

I want to convert like:

<Details>
<Uniqueno>11111</Uniqueno>

<ROW num="1">
<Name>60821894</Name>
<Age>938338789</Age>
</ROW>
<ROW num="2">
<Name>60821894</Name>
<Age>938338789</Age>
</ROW>

</Details>

note:ROWSET element is removed

I want to remove the element using JDOM API?

Prabhath kesav
  • 428
  • 3
  • 6
  • 21
  • Rowset is not an attribute. It's an element. In order to remove it, you'll have to first save all of its content somewhere to restore it later. – Andrew Logvinov May 09 '12 at 11:04

2 Answers2

1

What have you already tried ?

  1. Find the Details tag (which is the document root)
  2. Find the ROWSET tag
  3. for each ROW tag in ROWSET call the detach() method on the node and append() this detached node to the Details tag.
  4. Delete the ROWSET tag.

With some sample code:

// 1
Element details = doc.getRootElement();
// 2
Element rowset = details.getChild("ROWSET");
// 3
for (Element row: rowset.getChildren()) {
    Element r = row.detach();
    details.appendChild(r);
}
// 4
details.removeChild(rowset);

Not tested, for more info check the JDOM API.

Alex
  • 25,147
  • 6
  • 59
  • 55
1

If you are using JDOM 2.0.x you can do something like:

for (Element rowset : details.getChildren("ROWSET")) {
    rowset.detach();
    for (Content c : rowset.getContent()) {
         details.addContent(c.detach());
    }
}

If you are using JDOM 1.x you can do something similar, but with more casts....

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Thanks for the above postings,but I am still not able to detach(remove) using JDOM 1.x using detach or remove.How come I remove in version 1.x's? – Prabhath kesav May 09 '12 at 12:09
  • you can detach in 1.x, you just have to have the right casts... http://jdom.org/docs/apidocs.1.1/org/jdom/Content.html#detach%28%29 – rolfl May 09 '12 at 12:17