Skip navigation
Currently Being Moderated

Depth of a node in XML

Jan 4, 2011 10:59 PM

Does Flex provide any way to find out the depth of a given node in an XML.

 

For example, if XML is

 

<?xml version="1.0" encoding="ISO-8859-1"?>

<root>

<node label="Session">              

    <node id="session_query1" label="DTM Buffer Size" />

      <node id="session_query2" label="Buffer Block Size"/>

      <node id="session_query3" label="Enable Test Load"/>

</node>

    <node label="Workflow" >

    <node id="workflow_query1" label="Enable HA Recovery"/>

     <node id="workflow_query2" label="Suspend on Error"/>

</node>

    <node label="Mapping" >

    <node id="mapping_query1" label="SQL Override in SQ " />

     <node id="mapping_query2" label="SQL Override in lookup" />               

</node>

</root>

 

Assuming that root node is at depth 0, I should get the depth of the highlighted node as 2.

 

Thanks in Advance

 
Replies
  • Currently Being Moderated
    Jan 5, 2011 3:09 AM   in reply to Mohd Fayaz

    Why don;t you pass it from server as an attribute to your XML?

     

    C

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 3:38 AM   in reply to Mohd Fayaz

    What are you trying to achieve with this?

     

    C

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 3:51 AM   in reply to Mohd Fayaz

    Your XML has kind of the same structure so the depth of the child is always 2.

    So if you click on a certain item in the tree you can access this information

     

    e.g. id and label and also

    the parent label. Tha should be enough to identify what operation to perform,

    unless I am missing something.

     

    C

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 4:13 AM   in reply to Mohd Fayaz

    Hi Mohd Fayaz,

     

    what would be the depth of the node in this case..based on the higlighted item..??

     

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <root>

    <node label="Session">              

        <node id="session_query1" label="DTM Buffer Size" />

          <node id="session_query2" label="Buffer Block Size"/>

          <node id="session_query3" label="Enable Test Load"/>

    </node>

        <node label="Workflow" >

        <node id="workflow_query1" label="Enable HA Recovery"/>

         <node id="workflow_query2" label="Suspend on Error"/>

    </node>

        <node label="Mapping" >

        <node id="mapping_query1" label="SQL Override in SQ " />

         <node id="mapping_query2" label="SQL Override in lookup" />               

    </node>

    </root>

     

    Also based on which attribute or value you want to identify the depth of the node..??

     

    Is it based on the id attribute or the name attribute of the node..??

     

     

    Thanks,

    Bhasker

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 4:34 AM   in reply to Mohd Fayaz

    I am sorry Still I did not get exactly the depth concept you're talking about. Well make me clear with this..

     

    Tell me depths of all the below highlighted items..??

     

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <root>

    <node label="Session">           

          <node id="session_query1" label="DTM Buffer Size" />

          <node id="session_query2" label="Buffer Block Size"/>

          <node id="session_query3" label="Enable Test Load"/>

    </node>

    <node label="Workflow" >

        <node id="workflow_query1" label="Enable HA Recovery"/>

        <node id="workflow_query2" label="Suspend on Error"/>

    </node>

    <node label="Mapping">

        <node id="mapping_query1" label="SQL Override in SQ " />

        <node id="mapping_query2" label="SQL Override in lookup" />

        <node id="mapping_query3" label="SQL Override" />               

    </node>

    </root>

     

    What do you mean exactly by "I want to find depth relative to the root node of XML"..?? Whether you are trying to find it relative to the root node of the highlighted xml above which is <node /> tag or else main root node which is the root for the whole xml <root />

     

    Also you did not answer my question based on which attribute you want to know the depth of node..?? Is it based on name or id..??

     

    Thanks,

    Bhasker

     

     

    Thanks,

    Bhasker

     

    Message was edited by: BhaskerChari

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 9:17 AM   in reply to BhaskerChari

    I don't think there is any way to do this in a simple manner in Flex.

     

    I think all you can do is find the node in the data somehow, perhaps by walking down the XML, and keeping track of what level you are currently at.

     

    The problem is, what if there are duplicate nodes? For example in the following case the bold nodes are duplicates, but one set are at level 2 and the other set are at level 3 (root node at level 1):

     

    <root>

    <node label="Session1">          

          <node id="session_query1" label="DTM Buffer Size" />

          <node id="session_query2" label="Buffer Block Size"/>

          <node id="session_query3" label="Enable Test Load"/>

    </node>

    <node label="Session2">          

          <node label="sub_Session2">          

                <node id="session_query1" label="DTM Buffer Size" />

                <node id="session_query2" label="Buffer Block Size"/>

                <node id="session_query3" label="Enable Test Load"/>

          </node>

          <node id="session_query1" label="DTM Buffer Size" />

          <node id="session_query2" label="Buffer Block Size"/>

          <node id="session_query3" label="Enable Test Load"/>

    </node>

    </root

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 5, 2011 11:56 PM   in reply to Gregory Lafrance

    If I were to write code to do this, I would start at the child item and go to its parent and its parent etc, counting how many times I did that, until I reached the root node.  I'm assuming you start out knowing both the child node and the root node.

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 20, 2012 2:34 AM   in reply to Mohd Fayaz

    May be this could be of some help for some one still searching for the depth of the node in the tree.

     

    private function handleTreeItemClickEvent(event:ListEvent):void {

         trace(TreeListData(TreeItemRenderer(mytreeSource.itemToItemRenderer(e vent.currentTarget.selectedItem)).listData).depth);

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points