[jdom-interest] jdom-b7: Unable to replace an element
Crook, Charles
CrookC at tvratings.com
Thu Jul 26 10:59:49 PDT 2001
I'm trying to replace an existing element, but getting an exception that the
element already has a parent. I've checked to see if getParent() is null or
not before adding the element back,
and the element has already been detach()'d and clone()'d.
So why does it still have a parent?
Charlie Crook
Nielsen Media Research
source:
public boolean replaceElement( Element xmlReplace, String elemName ) throws
AdviewsException {
boolean bReturn = false ;
Element searchElem = null ;
String searchName = elemName.trim().toLowerCase() ;
Syslog.debug( this, "Adding " + searchName + ": " +
xmlReplace.toString() ) ;
try {
Syslog.debug( this, this.toXMLString() ) ;
List childList = this.getRootElement().getContent() ;
if ( childList.isEmpty() ) { Syslog.warning( this, "Unable to
find children of the root." ) ;
}else {
Syslog.debug( this, childList.toString() ) ;
int searchIndex = -1 ;
Iterator itr = childList.iterator();
while (itr.hasNext()) {
Object obj = itr.next();
if (obj instanceof Element) {
Element element = (Element)obj;
if (element.getName().equals(searchName) ) {
searchIndex = childList.indexOf(
element ) ;
element= (Element)childList.remove(
searchIndex ) ;
element.addContent( xmlReplace ) ;
Element replaceElem =
(Element)element.detach().clone() ;
if ( replaceElem.getParent() != null
) {
Syslog.debug( this, elemName
+ "'s parent is: " + replaceElem.getParent().getName() ) ;
}else {
Syslog.debug( this, elemName
+ " has no parent" ) ;
}
childList.add( searchIndex,
replaceElem );
Syslog.debug( this, "New content: "
+ childList.toString() ) ;
exception occurs here =======>
this.getRootElement().setContent( childList ) ;
bReturn = true ;
break ;
}else{
Syslog.warning( this, "Unable to
find " + searchName + " element" ) ;
}
}
}
}
} catch (Exception ex) {
throw new AdviewsException( ex,
"AdviewsDocument.replaceElement ",
AdviewsException.ERROR ) ;
}
return bReturn ;
}
debug logs:
Thu Jul 26 2001 at 12:50:32:601 PM GMT-05:00 [DBUG] [AdvWeb ] d2adv
AdviewsXMLResponse <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE
conversation SYSTEM "http://localhost:7001/advweb/style/nmr_adviews.dtd">
<conversation><response /><request /></conversation>
Thu Jul 26 2001 at 12:50:32:601 PM GMT-05:00 [DBUG] [AdvWeb ] d2adv
AdviewsXMLResponse [[Element: <response/>], [Element: <request/>]]
Thu Jul 26 2001 at 12:50:32:601 PM GMT-05:00 [DBUG] [AdvWeb ] d2adv
AdviewsXMLResponse response has no parent
Thu Jul 26 2001 at 12:50:32:601 PM GMT-05:00 [DBUG] [AdvWeb ] d2adv
AdviewsXMLResponse New content: [[Element: <response/>], [Element:
<request/>]]
Thu Jul 26 2001 at 12:50:32:601 PM GMT-05:00 [EROR] [AdvWeb ] d2adv
AdviewsException org.jdom.IllegalAddException: The element "response"
could not be added as a child of "conversation": The element already has an
existing parent "conversation"
exception trace:
at org.jdom.Element.addContent(Element.java:1176)
at org.jdom.Element.setContent(Element.java:865)
at
com.nielsenmedia.advweb.xml.AdviewsDocument.replaceElement(AdviewsDocument.j
ava:354)
at
com.nielsenmedia.advweb.xml.AdviewsDocument.addResponse(AdviewsDocument.java
:299)
at
com.nielsenmedia.advweb.xml.AdviewsXMLResponse.addLayout(AdviewsXMLResponse.
java:38)
at
com.nielsenmedia.advweb.xml.AdviewsXMLResponse.addLayout(AdviewsXMLResponse.
java:57)
at com.nielsenmedia.advweb.control.Gatekeeper.doLogin(Gatekeeper.java:269)
at com.nielsenmedia.advweb.control.Gatekeeper.service(Gatekeeper.java:172)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
More information about the jdom-interest
mailing list