-2

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

<start>
    <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>
</start>

I am trying to semect nodes by:

List list= document.selectNodes("//element1[@timetime='555555']" );

but the list returns null. I wanted to change all the 3 values where time="555555".

Isn't there a way to directly go to that node.

please help.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
user234194
  • 1,683
  • 10
  • 38
  • 56

2 Answers2

4

to select the three values, use:

//name[@time='555555']/element1/value

If that returns null as well, there may be a default namespace involved and that means you need to show your entire XML.

XPath is flexible, if you want you can express the same like this:

//value[ancestor::name[1]/@time='555555']
Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

The XPath that you are using is looking for a time attribute equal to 555555 on element1. However, your time attributes are on the name nodes.

You could go either the way Tomalak suggested, or change it to:

//element1[../@time='555555']

This is looking for an element1 node with a parent who has a time attribute equal to 555555.

carols10cents
  • 6,943
  • 7
  • 39
  • 56
  • I see that you have updated your XML, but it's currently invalid-- the element1 node needs to be closed, and the element2 node needs a closing element2 node instead of an opening element1 node. The XPath I posted works if I fix those issues. I suggest making sure that the XML you're using is valid, and then start by trying to select the root node and looking at the results, then selecting a child of the root node and looking at the results, etc until you have what you are trying to select. – carols10cents Dec 16 '10 at 18:21