• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to replace repeating value in XML

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have 2 repeating attributes, which I need to replace one with another. i.e.

<doc>
<data>
<old_year>2012</old_year>
<old_year>2013</old_year>
<old_year>2014</old_year>
</data>
<data>
<new_year>2010</new_year>
<new_year>2010</new_year>
<new_year>2011</new_year>
</data>
</doc>




The following can only replace the first value, and not others. Can someone help me understand why the other nodes gets empty value? Thanks.



public void someFunction () {

XPathExpression expr = xpath.compile("count(//doc/data/old_year)");
Integer yearCount = ((Double) expr.evaluate(doc, XPathConstants.NUMBER)).intValue();

System.out.println("Total anne values: " + yearCount.intValue());
for (int i=0; i < yearCount.intValue(); i++) {
xpathReplace(doc,"/doc/data/new_year[" + i + "]","/doc/data/old_year[" + i + "]");
}
}

private void xpathReplace(Document doc, String targetXpath, String sourceXpath) throws XPathExpressionException
{
Node sourceNode = null ;
Node targetNode = null ;
if(factory==null)
{
factory = XPathFactory.newInstance();
}
if(xpath==null)
{
xpath = factory.newXPath();
}

System.out.println("Sourc path: " + sourceXpath);
System.out.println("Target path: " + targetXpath);

sourceNode = (Node)xpath.evaluate(sourceXpath, doc,XPathConstants.NODE);
targetNode = (Node)xpath.evaluate(targetXpath, doc,XPathConstants.NODE);

if(sourceNode!=null && targetNode!=null )
{
targetNode.setTextContent(sourceNode.getTextContent());
}
}





 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The following can only replace the first value, and not others. Can someone help me understand why the other nodes gets empty value?


On the face of it, the code seems to intend to replace the new_year text by the correspondance, in position, old_year text value. It should run fine except that the last replacement wouldn't take place because the position() is 1-based rather than 0-based.

If you replace this

xpathReplace(doc,"/doc/data/new_year[" + i + "]","/doc/data/old_year[" + i + "]");


by this

the replacement will be complete.

But is that the problem or what you desire to happen?
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic