Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes XML and Related Technologies and the fly likes iterate a doc with sax and change xpath expression at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "iterate a doc with sax and change xpath expression at run time" Watch "iterate a doc with sax and change xpath expression at run time" New topic
Author

iterate a doc with sax and change xpath expression at run time

Arthur Burma
Greenhorn

Joined: Nov 12, 2009
Posts: 16
Hi, I d' like to evaluate an xpath expresion for getting the attribute value:



Here , I use attribute:value and get search / replace :




But, how to make the xpath expression to point directly for the value attribute: <property name="userDistrib" value="0007.02"/>

My goal is to modify this value in iterator, so a hardcoded value to replace is not possible ?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

I've typed two possible answers in here and erased both of them, because I can't tell what you are asking. I think you're going to have to explain this:
My goal is to modify this value in iterator, so a hardcoded value to replace is not possible ?

in some different way. Perhaps you could explain what that code is meant to do and what it does instead and why that's wrong.
Arthur Burma
Greenhorn

Joined: Nov 12, 2009
Posts: 16
That is getting a modification on xpath , each time an iteration is done:




so i have used this expression , but maybe a better way to set this independant of the order on attribute::value result
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
But XPath is going to use a DOM anyway, isn't it? Why are we talking SAX here?

What is GeneratorXpath?

Bill


Java Resources at www.wbrogden.com
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Arthur Burma wrote:so i have used this expression , but maybe a better way to set this independant of the order on attribute::value result

Well, assuming we are actually talking about DOM programming here (your code certainly looks like that): you should be getting an Attr node from that XPath expression. So why wouldn't you just use its setValue() method, as per the documentation?

I'm saying that based on the fact that you haven't told us that you are having a problem, or what it is if there is a problem. So I'm just guessing at things to say. So perhaps you could do that. If there are exceptions, show us the stack trace. If some output differs from what you think it should be, show it to us and tell us what the difference is.
Arthur Burma
Greenhorn

Joined: Nov 12, 2009
Posts: 16
To keep things simple I have to clarify , hereis the xml :


Actually I just want to modify the value in property with replacing the string "loader/CSV" by "output/local" for instance... I have some trouble <ith xpath for that because it's not outside an attribute tag;

regards
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18110
    
    8

Okay. So:

(1) Find the attribute.

(2) Extract its value into a String variable.

(3) Get the corrected value into a String variable.

(4) Set the new value of the attribute.

When you're done, don't forget to serialize your DOM. Changing the value in memory and not writing that out to a file can lead you to believe that the value wasn't changed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: iterate a doc with sax and change xpath expression at run time
 
Similar Threads
XML Entity reference
How to Generalize the code?
simple web service client
Retrieve values for a complicate XML file (Java)
Parsing an XML Document containing Namesapces using XPath and Xalan