问题
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?
回答1:
What have you already tried ?
- Find the
Details
tag (which is the document root) - Find the
ROWSET
tag - for each
ROW
tag inROWSET
call thedetach()
method on the node andappend()
this detached node to theDetails
tag. - 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.
回答2:
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....
来源:https://stackoverflow.com/questions/10514756/remove-xml-attribute-using-jdom-api