14 Replies Latest reply: Feb 20, 2012 2:34 AM by SmohsinK RSS

    Depth of a node in XML

    Mohd Fayaz Community Member

      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

        • 1. Re: Depth of a node in XML
          Mohd Fayaz Community Member

          Can anyone please help me. It is very urgent.

          • 2. Re: Depth of a node in XML
            Claudiu Ursica Community Member

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

             

            C

            • 3. Re: Depth of a node in XML
              Mohd Fayaz Community Member

              Thanks Claudiu for replying.

               

              I know this could be one solution. But, finding a depth of a node in XML, is a very trivial operation. I believe flex has some implementation for this. If this could be done in flex then it is waste to insert an extra column in XML.

              • 4. Re: Depth of a node in XML
                Claudiu Ursica Community Member

                What are you trying to achieve with this?

                 

                C

                • 5. Re: Depth of a node in XML
                  Mohd Fayaz Community Member

                  I am binding this XML to a tree component.

                  I have different set of operation to be performed for different level in the tree. I cant use node names, as node names are dynamic.

                  I think only way i can do it by finding the depth of currently selected node in the tree.

                  • 6. Re: Depth of a node in XML
                    Claudiu Ursica Community Member

                    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

                    • 7. Re: Depth of a node in XML
                      Mohd Fayaz Community Member

                      This is not the real XML I am going to use. It is different. However, I will check if what you have said is useful to me.

                       

                      Anyway, if you know of directly finding the depth of a given node, kindly let me know  me.

                      • 8. Re: Depth of a node in XML
                        BhaskerChari Community Member

                        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

                        • 9. Re: Depth of a node in XML
                          Mohd Fayaz Community Member

                          I want to find depth relative to the root node of XML.

                          Assuming, root node to be at depth 0, all the child nodes of root will be at depth 1, and their child nodes will be at depth 2 and so on.

                           

                          Therefore, the selected node depth should be 2.

                          • 10. Re: Depth of a node in XML
                            BhaskerChari Community Member

                            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

                            • 11. Re: Depth of a node in XML
                              Gregory Lafrance Community Member

                              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

                              • 12. Re: Depth of a node in XML
                                Wrataxas

                                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.

                                • 13. Re: Depth of a node in XML
                                  Mohd Fayaz Community Member

                                  Thanks All for you replies.

                                   

                                  I know it can be done in multiple ways. But I wanted to know whehter Flex has some built in method in XML Class or some other, to know this directly.

                                  Finding the depth at which a node is location in XML, is a very basic and essential feature.

                                   

                                  I hope Flex development team will take care of this in future releases.

                                  • 14. Re: Depth of a node in XML
                                    SmohsinK Community Member

                                    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(event.currentTarget.s electedItem)).listData).depth);

                                    }