Skip to main content
Known Participant
October 13, 2011
Answered

How to search attribute values

  • October 13, 2011
  • 2 replies
  • 908 views

How to find the attribute values ("0_0_0_1" ) in the following xml example.

Example:

var xml:XML=

<root>
<country id="0">
<states id="0_0">
<city id="0_0_0">
<village id="0_0_0_0"></village>
<village id="0_0_0_1"></village>
<village id="0_0_0_2"></village>
</city>
</states>
<states id="0_1">
</states>
</country>

<country id="1"></country>

</root>

This topic has been closed for replies.
Correct answer jasonsturges

Do you mean like a depth first search?

package

{

          import flash.display.Sprite;

          public class XmlAttributes extends Sprite

          {

                    protected var xml:XML = <root>

                                                                                          <country id="0">

                                                                                                    <states id="0_0">

                                                                                                              <city id="0_0_0">

                                                                                                                        <village id="0_0_0_0"></village>

                                                                                                                        <village id="0_0_0_1"></village>

                                                                                                                        <village id="0_0_0_2"></village>

                                                                                                              </city>

                                                                                                    </states>

                                                                                                    <states id="0_1">

                                                                                                    </states>

                                                                                          </country>

                                                                                          <country id="1"></country>

                                                                                </root>;

                    public function XmlAttributes()

                    {

                              super();

                              var result:XML = depthFirstSearch(xml);

                              trace(result.localName());

                    }

                    public function depthFirstSearch(node:XML):XML

                    {

                              for each (var child:XML in node.children())

                              {

                                        if (("@id" in child) && (child.@id == "0_0_0_1"))

                                                  return child;

                                        else

                                        {

                                                  var result:XML = depthFirstSearch(child);

                                                  if(result)

                                                            return result;

                                        }

                              }

                              return null;

                    }

          }

}

The XML tags in the code example above are being stripped - not sure why they cannot be encoded / escaped.  It is the XML from your example.

2 replies

jasonsturges
Inspiring
October 13, 2011

With e4x, you can use an expression to search for an id:

package
{
     import flash.display.Sprite;

     public class XmlAttributes extends Sprite
     {
          
          protected var xml:XML = <root>
                                             <country id="0">
                                                  <states id="0_0">
                                                       <city id="0_0_0">
                                                            <village id="0_0_0_0"></village>
                                                            <village id="0_0_0_1"></village>
                                                            <village id="0_0_0_2"></village>
                                                       </city>
                                                  </states>
                                                  <states id="0_1">
                                                  </states>
                                             </country>
                                             <country id="1"></country>
                                        </root>;

          public function XmlAttributes()
          {
               super();
               
               var node:XMLList = xml..village.(@id == "0_0_0_1");
                    
               trace(node.localName());
          }

     }
}

Is this what you're after?

kiranemc2Author
Known Participant
October 13, 2011

It is OK. But i want to search the Attribute value ("0_0_0_1") it returns to Show the Node tage.

jasonsturges
jasonsturgesCorrect answer
Inspiring
October 13, 2011

Do you mean like a depth first search?

package

{

          import flash.display.Sprite;

          public class XmlAttributes extends Sprite

          {

                    protected var xml:XML = <root>

                                                                                          <country id="0">

                                                                                                    <states id="0_0">

                                                                                                              <city id="0_0_0">

                                                                                                                        <village id="0_0_0_0"></village>

                                                                                                                        <village id="0_0_0_1"></village>

                                                                                                                        <village id="0_0_0_2"></village>

                                                                                                              </city>

                                                                                                    </states>

                                                                                                    <states id="0_1">

                                                                                                    </states>

                                                                                          </country>

                                                                                          <country id="1"></country>

                                                                                </root>;

                    public function XmlAttributes()

                    {

                              super();

                              var result:XML = depthFirstSearch(xml);

                              trace(result.localName());

                    }

                    public function depthFirstSearch(node:XML):XML

                    {

                              for each (var child:XML in node.children())

                              {

                                        if (("@id" in child) && (child.@id == "0_0_0_1"))

                                                  return child;

                                        else

                                        {

                                                  var result:XML = depthFirstSearch(child);

                                                  if(result)

                                                            return result;

                                        }

                              }

                              return null;

                    }

          }

}

The XML tags in the code example above are being stripped - not sure why they cannot be encoded / escaped.  It is the XML from your example.

kglad
Community Expert
Community Expert
October 13, 2011

xml..village.(@id=="0_0_0_1") references the village node with that id.