-2

Possible Duplicate:
XPATH problem with dom4j

I am using dom4j to overwrite a value in the XML. The XML looks like this:

<name color="blue" time="555555"> 
    <element1 param="1"> 
        <value>value1</value> 
        <value>value2</value> 
        <value>value3</value> 
    <element1> 
</name> 

<name color="blue" time="888888"> 
    <element2 param="1"> 
        <value>value1</value> 
        <value>value2</value> 
        <value>value3</value> 
    <element1> 
</name> 

I am trying to select nodes by:

SAXReader saxReader = new SAXReader();
Document document =  saxReader.read(xmlLocation);
List list= document.selectNodes("//element1/@color/[@time='555555']" );

but the list returns boolean(which is true in this case). I wanted to change all the 3 values where time="555555".

If I do:

List list= document.selectNodes("//element1/@time" );

It returns nodes.(attributes and elements) Isn't there a way to directly go to that node where time is 555555. please help.

Community
  • 1
  • 1
user234194
  • 1,683
  • 10
  • 38
  • 56
  • read both questions properly and comment it again.I know what is duplicate question means. – user234194 Dec 16 '10 at 17:46
  • your xml is flawed.. you do not close `element1` nor `element2` – Gabriele Petrioli Dec 16 '10 at 17:49
  • The input XML you showed is not well-formed (multiple top-level elements), so you should not even be able to get it parsed in order to get a result from selectNodes(). The first XPath you showed has a syntax error, and so could not return a boolean value. If you want to select the node whose time attribute has a value of 555555, use `"//*[@time = '555555']"`. This will be a `` element. – LarsH Dec 16 '10 at 17:52
  • Maybe I didn't get the question, but shouldn't it be `//name[@time = '555555']/*/value` ? `//element1/@color/[@time='555555']` is invalid, I think. – khachik Dec 16 '10 at 17:52
  • your xpath is also malformed. can you provide a little more on what you're attempting? Are you trying to find element1 nodes that have a color attribute and a time attribute that is equal to 555555? – Dave G Dec 16 '10 at 17:54
  • Lets assume that the XML is in correct syntax. My question is when ever i use @time='555555', list returns boolean value. if I use "element1/@time" , it returns list of elements/attributes. – user234194 Dec 16 '10 at 18:00
  • 1
    Your questions show that you definitely need to read a good book on XPath to grasp at least the fundamentals. Sporadic questions at forums aren't going to help. – Dimitre Novatchev Dec 16 '10 at 18:06
  • @khachik, if you were asking about my XPath expression, I was answering the question "Isn't there a way to directly go to that node where time is 555555". One of multiple q's the OP asked. – LarsH Dec 16 '10 at 18:12
  • @user234194: `My question is when ever i use @time='555555', list returns boolean value. if I use "element1/@time" , it returns list of elements/attributes.` That's not a question. Also what do you mean list returns boolean value? list is a variable of type List. It cannot return anything, and its value is of type List. Show us what it is that convinces you that the value it holds is a boolean. – LarsH Dec 16 '10 at 18:13
  • -1 for posting inaccurate data and code that makes helping 10x more complicated, unnecessarily. – LarsH Dec 16 '10 at 18:27

1 Answers1

2

Assuming the following well-formed document:

<names>
    <name color="blue" time="555555"> 
        <element1 param="1"> 
            <value>value1</value> 
            <value>value2</value> 
            <value>value3</value> 
        </element1> 
    </name> 
    <name color="blue" time="888888"> 
        <element1 param="1"> 
            <value>value1</value> 
            <value>value2</value> 
            <value>value3</value> 
        </element1> 
    </name> 
</names>

If you want all 'name' nodes with a time of 555555 then you'll need

//name[@time = '555555']

If you want all value nodes underneath name with a time of 555555 then you'll need

//name[@time = '555555']/element1/value
Dave G
  • 9,639
  • 36
  • 41
  • //name[@time = '555555'] ...using this I get boolean vaue of true. – user234194 Dec 16 '10 at 18:05
  • +1 for somehow managing to meet the OP's need, despite the static. – LarsH Dec 16 '10 at 18:29
  • @LarsH: I don't think we should encourage the same answer to the same question... –  Dec 16 '10 at 21:43
  • @Alej: point taken. The OP seemed to be arguing that it wasn't a duplicate if we "read both questions properly", and I didn't take the time to read them both carefully. – LarsH Dec 16 '10 at 22:10