A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: How do I access XML node NAMES?

  1. #1
    Senior Member
    Join Date
    Feb 2008
    Posts
    107

    How do I access XML node NAMES?

    I'll try to explain what I want to do in plain english, because I'm having trouble working it out in AS...

    The following XML represents the structure of a website. First I go through all the "second level" nodes to determine which of them has a "subNavName" node in it - this tells me whether that section of the site has subnav. That part I have worked out:
    Code:
    for each (var nodeI:XML in contentXML.*)
    {
    	if (nodeI.subNavName) // this site section has a node called 'subNavName'
    	{
    		
    	}
    }
    Then I need to get all the OTHER nodes at the SAME LEVEL within that "second level" node. In the "works" state these are "vancouverRiverfront" and "progressRidge". I need to get the NAMES of these elements (i.e. I need to get "vancouverRiverfront" and "progressRidge") - I can't figure out how to a) look at all the elements that are one level "down" within the "works" state and b) get the node names, to determine 1) which one is "subNavName" and so doesn't need any further retrieving, and 2) which ones are NOT "subNavName", and get their name, and continue tunneling down to retrieve other content (which I CAN do).

    Everything I read tells me how to access node CONTENT, but not node names within a given node. Is there a practical way to do this, or should I not be naming my nodes with a string I want to be able to retrieve?

    The XML:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <content>
    	<home>
    		<elements>
    			<element>
    				<text>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis.</text>
    			</element>
    			<element>
    				<text>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis.</text>
    			</element>
    			<element>
    				<text>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis.</text>
    			</element>
    		</elements>
    	</home>
    	
    
    <!-- WORKS state -->
    
    	<works>
    		<subNavName>subNavWorks</subNavName>
    		<vancouverRiverfront>
    			<subNavBtnName>btnWorksVancouver</subNavBtnName>
    			<subNavBtnTitle>Vancouver Waterfront</subNavBtnTitle>
    			<stateClass>SlideshowState</stateClass>
    			<title>Vancouver Waterfront Master Plan</title>
    			<subtitle>Subtitle</subtitle>
    			
    			<body>
    				<textBlock>
    					<content>Some Text</content>
    				</textBlock>
    			</body>
    		</vancouverRiverfront>
    		
    		<progressRidge>
    			<subNavBtnName>btnWorksProgress</subNavBtnName>
    			<stateClass>SlideshowState</stateClass>
    			<title>Progress Ridge</title>
    			<subtitle>Subtitle</subtitle>
    			
    			<body>
    				<textBlock>
    					<content>Some text</content>
    				</textBlock>
    			</body>
    		</progressRidge>
    	</works>
    	
    	<philosophy>
    		<subNavName>subNavPhilosophy</subNavName>
    		<studies>
    			<subNavBtnName>btnPhilosophyStudies</subNavBtnName>
    			<stateClass>RectangularTransformState</stateClass>
    			<title>Design Studies</title>
    
    			<body>
    				<textBlock>
    					<content>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis. Integer in ipsum. Aliquam erat volutpat. Suspendisse sapien. Aliquam erat volutpat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.</content>
    				</textBlock>
    				<textBlock>
    					<content>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis. Integer in ipsum. Aliquam erat volutpat. Suspendisse sapien. Aliquam erat volutpat.</content>
    				</textBlock>
    			</body>
    		</studies>
    		<thinking>
    			<title>Visual Thinking</title>
    
    			<body>
    				<textBlock>
    					<content>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis. Integer in ipsum. Aliquam erat volutpat. Suspendisse sapien. Aliquam erat volutpat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.</content>
    				</textBlock>
    			</body>
    		</thinking>
    		<planning>
    			<title>Planning</title>
    
    			<body>
    				<textBlock>
    					<content>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut in magna quis enim placerat elementum. In magna. Nam dignissim turpis in dolor. Donec sit amet leo et nulla consequat iaculis. Integer in ipsum. Aliquam erat volutpat. Suspendisse sapien. Aliquam erat volutpat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos.</content>
    				</textBlock>
    			</body>
    		</planning>
    	</philosophy>
    </content>

  2. #2
    Senior Member
    Join Date
    Apr 2006
    Posts
    129
    PHP Code:
    for each (var nodeI:XML in contentXML.*) {
        for 
    each (var nodeJ:XML in nodeI.*) {
            if (
    nodeJ.name() == "subNavName"trace ("aaa "+nodeJ.name())
            else 
    trace ("bbb "+nodeJ.name())
        }


  3. #3
    Senior Member
    Join Date
    Feb 2008
    Posts
    107
    Right on! I thought it had to be something easy, but I was trying to do it as either a property (nodeJ.name) or a "get" function (nodeJ.getName()).

    One more question - my first conditional - "if (nodeI.subNavName)" - doesn't seem to be limiting me to just the nodeI's that contain a node called subNavName. When I do:
    PHP Code:
    if (nodeI.subNavName// this site section has a node called 'subNavName'
    {
        for 
    each (var nodeJ:XML in nodeI.*)
        {
            if (
    nodeJ.name() != 'subNavName')
            {
                
    trace(nodeJ.name());
            }
        }

    on the XML above I get:
    PHP Code:
    elements
    vancouverRiverfront
    progressRidge
    studies
    thinking
    planning 
    - but I shouldn't be getting the first one - "elements" - because the "home" node doesn't contain a node called 'subNavName'.

    Or rather - that's how I want it to work. But I seem to be doing something wrong. How should I be asking "if this node has a node called 'subNavName', then look at each node in this node - otherwise don't"?

    Thanks!

  4. #4
    Senior Member
    Join Date
    Apr 2006
    Posts
    129
    easy way is to skip the first node:

    PHP Code:
    for (var i:uint 1contentXML.children().length(); i++) {
        var 
    nodeI:XML contentXML.children()[i];
        for 
    each (var nodeJ:XML in nodeI.*) {
            if (
    nodeJ.name() != "subNavName"trace (nodeJ.name())
        }


  5. #5
    Senior Member
    Join Date
    Feb 2008
    Posts
    107
    Ah - but it needs to be more flexible. In this case I have another section (not shown) after the XML shown that doesn't have subnav. I really need to determine which top-level nodes _have_ subnav, and only proceed on those.

  6. #6
    Senior Member
    Join Date
    Feb 2008
    Posts
    107
    Got it:
    PHP Code:
    if (nodeI.subNavName.toString().length 0

  7. #7
    Senior Member
    Join Date
    Apr 2006
    Posts
    129
    If you'll keep subNavName in first position

    PHP Code:
    for each (var nodeI:XML in contentXML.*) {
        if (
    nodeI.children()[0].name() == "subNavName") {
            for 
    each (var nodeJ:XML in nodeI.*) {
                if (
    nodeJ.name() != "subNavName"trace (nodeJ.name())
            }
        }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center