<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC-html40"
xmlns:ns1="" xmlns:ns0="urn:schemas-microsoft-com:office:smarttags">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="PostalCode"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="State"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="City"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="place"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="Street"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
name="address"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:Arial;
        color:windowtext;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
        {page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Hi All,<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I’m running JDOM 1.0 and I just ran into an
interesting corruption problem. In my code, I’m parsing XML elements in a
doc and removing specific elements (branches) based on their attribute values.
Please see the code snippet below.<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>I initially call this code with profileDOM(null,doc.getRootElement()).
It recursively parses the tree until my checkAttributes() method returns false.
At this point, it removes that element. If it doesn’t return false then
it gets the list of children and calls itself (recursion) for each child.<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>The problem occurs in the removeContent call. The
removeContent correctly removes the content, but appears to screw up the list.
The second time that removeContent is called with a different parent, it
removes the wrong content node. It appears that the internal list somehow gets
screwed up. While I was debugging this in my IDE, I took a look at the content
list in the parent element and the parent.indexOf(node) was returning the wrong
index for the actual content. In my sample, it returned a text node instead of
an element!<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>So, two questions –<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>If this isn’t the correct type of code to prune
content, what is? I remember trying an iterator a while back, but ended up
getting some exception.<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>The second is, isn’t this a bug? Is this the right
forum to report it?<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> public void profileDOM(Element parent,
Element node) throws Exception<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> {<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> if (parent !=
null && !checkAttributes(node))<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> {<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> //
previously just doing parent.removeContent(node);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
int index = parent.indexOf(node);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
Content content = parent.removeContent(index);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> if
(content == null)<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
{<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
logger.info("Removal of element <" + node.getName() + ">
failed!");<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
}<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> }<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> else<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> {<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
List list = node.getChildren();<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
for(int i=0;i<list.size();i++)<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
{<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
Element child = (Element)list.get(i);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
// check to see if we should delete child<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
profileDOM(node,child);<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>
}<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> }<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'> }<o:p></o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'><o:p> </o:p></span></font></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Thanks,<o:p></o:p></span></font></p>
<p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><font
size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'>Jeff Rosler<br>
Consultant<br>
Flatirons Solutions Corporation<br>
XML and Content Management Solutions<br>
<ns0:Street w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z"><ns0:address
w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z"
ns1:tabIndex="0"
ns1:style="BACKGROUND-POSITION: left bottom; BACKGROUND-IMAGE: url(res://ietag.dll/#34/#1001); BACKGROUND-REPEAT: repeat-x"><st1:Street
w:st="on"><st1:address w:st="on">2555 55th Street</st1:address></st1:Street></ns0:address></ns0:Street><br>
<ns0:address w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z"
ns1:tabIndex="0"
ns1:style="BACKGROUND-POSITION: left bottom; BACKGROUND-IMAGE: url(res://ietag.dll/#34/#1001); BACKGROUND-REPEAT: repeat-x"><ns0:Street
w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z"><st1:address
w:st="on"><st1:Street w:st="on">Suite</st1:Street></ns0:Street></st1:address>
100D</ns0:address><br>
<ns0:place w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z"><ns0:City
w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z">Boulder</ns0:City>,
<ns0:State w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z">CO</ns0:State>
<ns0:PostalCode w:insAuthor="Jeff Rosler" w:insDate="2005-11-09T07:43:00Z"
w:endInsAuthor="Jeff Rosler" w:endInsDate="2005-11-09T07:43:00Z">80301</ns0:PostalCode></ns0:place><br>
303-544-0514 x111<br>
<a href="mailto:jeff.rosler@flatironssolutions.com"
title="mailto:jeff.rosler@flatironssolutions.com">jeff.rosler@flatironssolutions.com</a></span></font><o:p></o:p></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>"The man with a new idea is a crank until the idea
succeeds"</span></font><o:p></o:p></p>
<p class=MsoNormal><font size=2 face=Arial><span style='font-size:10.0pt;
font-family:Arial'>Mark Twain</span></font><o:p></o:p></p>
<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p> </o:p></span></font></p>
</div>
</body>
</html>