PDA

View Full Version : searching E4X hierarchy with strings?


rforte
01-07-2008, 02:46 PM
I have an app that takes, as string input, the path to an XML node. For example:
user enters: node.field.child.setting0
Based on this string i want to be able to look it up in oan XML object. Is there a method in the XML object to do this or do I have to split up each node in the string and call xml.elements() on it?

Ideally I'd like to do:
var xmlQuery:String = "node.field.child.setting0";
var resultXML:XMLList = myXML.elements( xmlQuery );
or even:
resultXML = myXML[ xmlQuery ];

thx

hangalot
01-07-2008, 02:56 PM
this is the bad of e4x.
if you use e4x you need to use

node["string"]["string"]

i would personally recomend using xpath as it is a better fit for that kind of problem.
http://code.google.com/p/xpath-as3/

rforte
01-07-2008, 03:39 PM
Cool. That works.

Had problem with the swc though:
mxmlc -external-library-path=xpath-as3-0.2.4a.swc main.mxml
Loading configuration file ../flex_sdk_2/frameworks/flex-config.xml
Error: unable to load SWC xpath-as3-0.2.4a.swc: unknown element script found in digests section in catalog.xml.

instead i had to source-path to the source.

hangalot
01-07-2008, 03:41 PM
yeah, the swc's for flex3 and flex2 differ although they get compiled from the same source

rforte
01-08-2008, 08:57 AM
I'm hoping you can help me out with one more issue. I tried updating some XML based on the following link: http://groups.google.com/group/xpath-as3/browse_thread/thread/7929b1384b6bcc5b?hl=en-GB However, this doesn't update the source XML. It only updates the XML returned from the XPath query. This would suggest that the XPath query returns a copy of the result rather than a reference to the source XML. Here's my code:


private function ChangeXML() : void
{
var document:XML = <document>
<location>Montrose</location>
<timeout time="87">
<value>123</value>
</timeout>
<voltage mtime="32" id="input">17.6</voltage>
</document>;

var xpq:XPathQuery = new XPathQuery("/document/location[1]")
var node:XML = xpq.exec(document)[0];

node.parent().children()[node.childIndex()] = "abc";

trace( 'ChangedXML ' + document );
trace( 'node ' + node.toXMLString() );
}


The result is that the first trace shows the original XML. The second trace shows: 'node <location>abc</location>'. From the google post I would have believed that the source XML would have been updated.