Hi Marc

Finding nodes does not remove them from the document, so you can find or
iterate over, say elements, then change them, adding/removing nodes, adding
attributes and so forth fine.

Taking a look at your code I think that should be fine. There's no reason to
dteach() a node from its parent then put it back again - just modify it
directly.

You could do something like this...

Element address = (Element) doc.selectSingleNode( "//address[@key='123']" );
address.addAttribute( "cid", "456" );

James
----- Original Message -----
From: "Marc Elliott" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, March 02, 2002 4:23 PM
Subject: [dom4j-user] manipulating elements/nodes "in place"


Hello folks,

I have some code that is supposed to look in a dom4j node, find child
elements/nodes within it that fit a criteria, then add an attribute to it.
This would be pretty simple except that this is unstructured content so when
the altered element is put "back" into the document it needs to go back to
exactly the place where it was before.

Is there a way I can manipulate the matched elements without pulling it out
of the parent node?

This is the code, I end up with a properly edited element, but the element
isn't part of the parent, intro, element anymore.

Document introDoc = DocumentHelper.parseText("<intro>" +
newsletter.getIntro() + "</intro>");
Element intro = introDoc.getRootElement();
// find all address values and append cid values

List addressList = intro.selectNodes("//address");
for (Iterator iter = addressList.iterator(); iter.hasNext(); ) {
Element addressEl = (Element) iter.next();
Enumeration articleListEnum = newsletter.getList().elements();
while(articleListEnum.hasMoreElements()){
      ArticleList articleList = (ArticleList)
articleListEnum.nextElement();
if(articleList.getType().equals("article")){
Enumeration articles =
articleList.getMemberArticle().elements();
while(articles.hasMoreElements()){
                Article article = (Article)
articles.nextElement();

if(addressEl.attributeValue("key").equals(article.getAddress())){

addressEl.addAttribute("cid",String.valueOf(article.getContentArticleID()));
}
  }
}
}
}

The sample content I've been working with:

<intro>
<p>This edition of the <publication>News</publication> herald's plans
for growth by quite a few players in the high-net-worth financial services
sector.</p>

<p><company>State Street Corporation</company>, for example, debuted
<address type="veracityID" key="451T-JWF0-01CT-103R-00000-00">a full-service
family advisor product</address> for individuals and families with assets
under management of $50 million and up.  <address type="veracityID"
key="452W-4B60-01CT-124B-00000-00"><publication>American
Banker's</publication> short profile of <company>Wilmington
Trust's</company>
journey</address> from regional bank to national wealth management
powerhouse
implies a strategy of continuous growth. Others in the news with expansions
and talk of expansions include: <address type="veracityID"
key="4509-TXJ0-002N-P0X7-00000-00"><company>Scotiabank's</company> private
client group</address> with a set of seven new offices; <address
type="veracityID"
key="450H-G0P0-01CT-13MT-00000-00"><company>Mellon</company></address> with
talk about renewed growth in its retail divisions; <address
type="veracityID"
key="4521-SY60-01CS-M2S5-00000-00"><company>Neuberger</company></address>
with new real estate services for the wealthly.</p>  <intro>


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to