• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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?
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic