E4X Filtering XML

marcparillo
marcparillo used Ask the Experts™
on

I'm having the hardest time trying to figure out how to extract a particular node from an XML file using E4X filtering in Flash/Actionscript.

All I know is the <truck> id and I need to get the entire <plot> parent node based on that id.

For example:

var xmlList:XMLList = xml..plot.(trucks.truck.(id==The_id_I_Want));

So, if I need the plot for the truck whose id is 26, I would get the following xml snippet:

<plot woeid="2347584" location="Missouri">
  <latitude>38.2588940</latitude>
  <longitude>-92.4365880</longitude>
  <trucks>
    <truck>
      <id>26</id>
      <title><![CDATA[Main Truck]]></title>
      <operators>
        <permanent><![CDATA[JK Smith]]></permanent>
      </operators>
    </truck>
  </trucks>
</plot>

Thanks,
marc
<?xml version='1.0' encoding='utf-8'?>
<root>
<header><source>trucks</source>
</header>
<plotList>
<plot woeid="2512025" location="Vicksburg">
  <latitude>32.3500400</latitude>
  <longitude>-90.8815540</longitude>
  <trucks>
    <truck>
      <id>23</id>
      <title><![CDATA[Blizzard]]></title>
      <operators>
        <current><![CDATA[Mike Smith]]></current>
      </operators>
    </truck>
  </trucks>
</plot>
<plot woeid="2347584" location="Missouri">
  <latitude>38.2588940</latitude>
  <longitude>-92.4365880</longitude>
  <trucks>
    <truck>
      <id>26</id>
      <title><![CDATA[Main Truck]]></title>
      <operators>
        <permanent><![CDATA[JK Smith]]></permanent>
      </operators>
    </truck>
  </trucks>
</plot>
<plot woeid="2359991" location="Baton Rouge">
  <latitude>30.4433350</latitude>
  <longitude>-91.1869940</longitude>
  <trucks>
    <truck>
      <id>10</id>
      <title><![CDATA[Cowboy]]></title>
      <operators>
        <current><![CDATA[Mark Smith]]></current>
        <current><![CDATA[Mike Smith]]></current>
      </operators>
    </truck>
  </trucks>
</plot>
</plotList>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
Did you close </root> in your file?

<root>
	<header>
		<source>trucks</source>
	</header>
	<plotList>
		<plot woeid="2512025" location="Vicksburg">
		  <latitude>32.3500400</latitude>
		  <longitude>-90.8815540</longitude>
		  <trucks>
			<truck>
			  <id>23</id>
			  <title><![CDATA[Blizzard]]></title>
			  <operators>
				<current><![CDATA[Mike Smith]]></current>
			  </operators>
			</truck>
		  </trucks>
		</plot>
		<plot woeid="2347584" location="Missouri">
		  <latitude>38.2588940</latitude>
		  <longitude>-92.4365880</longitude>
		  <trucks>
			<truck>
			  <id>26</id>
			  <title><![CDATA[Main Truck]]></title>
			  <operators>
				<permanent><![CDATA[JK Smith]]></permanent>
			  </operators>
			</truck>
		  </trucks>
		</plot>
		<plot woeid="2359991" location="Baton Rouge">
		  <latitude>30.4433350</latitude>
		  <longitude>-91.1869940</longitude>
		  <trucks>
			<truck>
			  <id>10</id>
			  <title><![CDATA[Cowboy]]></title>
			  <operators>
				<current><![CDATA[Mark Smith]]></current>
				<current><![CDATA[Mike Smith]]></current>
			  </operators>
			</truck>
		  </trucks>
		</plot>
	</plotList>
</root>

Open in new window

Top Expert 2011
Commented:
var The_id_I_Want:String = "26";
var xmlList:XMLList = xml.plotList.plot.trucks.truck.(id==The_id_I_Want);
trace(xmlList);

this code returns:

<truck>
  <id>26</id>
  <title><![CDATA[Main Truck]]></title>
  <operators>
    <permanent><![CDATA[JK Smith]]></permanent>
  </operators>
</truck>

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial