PDA

View Full Version : Find data node at arbitrary depth?


tspratt
12-22-2003, 05:01 PM
I have an xml data structure that is arbitrarily deep. I know a unique id for the node. Is there a simple way to find (set a datapointer to) that node?

The xpath "//mynode[foo='bar']" does not seem to work. Is it supported?

Also, can I use searchSubnodes() on a data node and is it recursive, or only a single level?

Thanks

antun
12-29-2003, 10:28 AM
You would have to write a recursive method to do the job for you. Something like:


<canvas debug="true" height="800">
<debug y="30" height="500" />
<dataset name="myds">
<myroot>
<mynode id="4" />
<mynode id="3" />
<mynode id="74" />
<mynode id="72">
<mynode id="735" />
<mynode id="83">
<mynode id="123" name="This is the special node" />
</mynode>
</mynode>
</myroot>
</dataset>

<datapointer name="mypointer" xpath="myds:/">
<method name="findNode" args="uniqueId">
Debug.write( "Searching for: " + uniqueId );
this.setAttribute( "uniqueId", uniqueId );
return this.recursor();
</method>

<method name="recursor">
do {
if ( this.getXPath("@id")==this.uniqueId ) {
Debug.write( "-------------- FOUND" );
Debug.write( this.getXPath("@name") );
return this;
}
if ( this.selectChild() ) {
this.recursor();
}
} while( this.selectNext() );
return null;
</method>
</datapointer>

<button onclick="mypointer.findNode( '123' )">Find that node now
</button>
</canvas>


... should do it.


The xpath "//mynode[foo='bar']" does not seem to work. Is it supported?


Not yet - predicate function support is coming in the next release. However it only searches through siblings, which is what the XPath spec recommends, as far as I know.


Also, can I use searchSubnodes() on a data node and is it recursive, or only a single level?


Not sure - it didn't work for me at all. I'll look into whether it's a bug or not.

-Antun

antun
12-29-2003, 11:17 AM
There's no API for retrieving the current cursor (I've field a feature request though), although you could write your own setter method that saved the cursor, before setting it:


<canvas debug="true">
<script>
LzCursor.mySetCursor = function( what ) {
LzCursor.setAttribute( "currentCursor", what );
LzCursor.setCursorGlobal( what );
}
</script>

<resource name="mycursor" src="foo.jpg" />

<simplelayout spacing="15" />
<button>Set Cursor Using special method
<method event="onclick">
LzCursor.mySetCursor('mycursor');
</method>
</button>

<button>Retrieve Cursor
<method event="onclick">
Debug.write( "cursor: " + LzCursor.currentCursor );
</method>
</button>

</canvas>


-Antun

tspratt
12-30-2003, 05:17 PM
I have created a recursive function that does what I need. Yours is simpler, though and I will look at it closely.

I couldn't make searchSubnodes work on a data node, but the recursive function does the job instead.

I think your response on the LZCursr should have gone to epopov's thread. (s)he'll probably find it here ok.

Thanks,
Tracy