dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: help with xml...pleeeeeeeeeease

Hybrid View

  1. #1
    Member
    Join Date
    Nov 2003
    Posts
    67

    help with xml...pleeeeeeeeeease

    Ok. here's the deal.

    I have a XML file structured like this.

    Code:
    >xml version="1.0" encoding="iso-8859-1"?<
    >product<
    this text is displayed in flash
    >/product<
    and i have the following code in frame 1 in flash.

    Code:
    //init TextArea component
    myProducts.html = true;
    myProducts.wordWrap = true;
    myProducts.multiline = true;
    
    //load in XML
    productContent = new XML();
    productContent.ignoreWhite = false;
    productContent.load("products.xml");
    productContent.onLoad = function(success)
    {
            if(success)
            {
                    myProducts.text = productContent;
            }
    }
    stop();
    The above code shows all the content from the xml file in my textArea. What i want to do is have in my xml code is a section for <products>, <instructions> and <video>. Now what do i have to do in flash to make sure i only target the <product> section of the code and not instructions or video at the same time?

    any help would be greatly appreciated.

  2. #2
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749

    Re: help with xml...pleeeeeeeeeease

    1. You had a number of errors in your XML, eg forgetting the leading "?" etc.
    2. You are not using the structure of XML to its best advantage. Use the node structure to group related items, then you can pass nodes around Flash using references. It makes for very powerful programming.
    3. To get you started, I have attached a very basic example that uses your original as a start point (and therefore does not necessarily represent the approach I would take, but is at least functional).

  3. #3
    Member
    Join Date
    Nov 2003
    Posts
    67
    Thanks for the help.

    But im having trouble displaying the "instructions" part of the XML.

    The trace shows that it is in fact loading, i just can''t display it. Same goes for video.

  4. #4
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    You just have to learn about walking the tree. Here are some facts about XML:

    There can only be one root node.
    Every node other than the root node has a parent.
    Nodes that share the same parent are siblings.
    Text values are nodes, they are children of the element node that contains them.
    You can distinguish between text nodes and element nodes by using the node.nodeType property where 1 is an XML element and 3 is a text node.
    A node is actually an object reference, so you can pass it around easily.

    Once you understand the above, it is easy to find what you want. For example, in the XML I sent you, the nodes are:
    details = rootnode
    products = first child of rootnode
    instructions = second child of rootnode
    video = third child of rootnode

    code:

    productContent.onLoad = function (success) {
    if(success) {
    // XML loaded successfully
    // The first node of a loaded XML must be the rootnode
    var rootnode = this.firstChild;
    // Product node is the first child of rootnode,
    // and the text value is the first child of that
    var productnode = rootnode.firstChild;
    var productnode_text = productnode.firstChild.nodeValue;
    // Instructions are the second child of rootnode
    // As an alternative to nextSibling, I could have used
    // rootnode.lastChild.previousSibling
    var instructionsnode = productnode.nextSibling;
    var instructionsnode_text = instructionsnode.firstChild.nodeValue;
    // Video is the last child, or the nextSibling of instructions
    var videonode = rootnode.lastChild;
    var videonode_text = videonode.firstChild.nodeValue;
    // Finally, you can set values based on what you have here:
    myProducts.text = "All three values are:"+productnode_text+" and "+instructionsnode_text+" and "+videonode_text;
    }
    }


  5. #5
    Member
    Join Date
    Nov 2003
    Posts
    67
    Thanks again. All your explanations have been very helpful.

    I'm trying to figure all this out still. Hopefully i get it.

  6. #6
    Member
    Join Date
    Nov 2003
    Posts
    67
    Thanks again. All your explanations have been very helpful.

    I'm trying to figure all this out still. Hopefully i get it.

  7. #7
    Member
    Join Date
    Nov 2003
    Posts
    67
    ok, im still lost.

    I can't get this.

  8. #8
    Member
    Join Date
    Nov 2003
    Posts
    67
    woohoo. I got it to work.

    I changed this line in the code.
    myProducts.htmlText = this.lastChild.lastChild.lastChild.nodeValue;

    and it worked.

    Why is it that if i modify the rest of the code to whatever, it doesnt affect the output at all?

  9. #9
    Member
    Join Date
    Nov 2003
    Posts
    67
    ok, new problem.

    I can get to te first "child" and last "child", but not the 2nd one.

    First child is accessed using:
    myProducts.htmlText = this.firstChild.firstChild.firstChild.nodeValue;

    and the last Child is accessed using
    myProducts.htmlText = this.lastChild.lastChild.lastChild.nodeValue;


    But i'm not sure of how to get to the second one.
    I figured it would be firstChild.firstChild.nodeValue;. But i was wrong.
    Any ideas?

  10. #10
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    If you think about it, if this is the entire XML that you loaded:

    this.lastChild.lastChild.lastChild.nodeValue;
    (XML.details . video .textnode .nodeValue);

    is exactly the same as:

    this.firstChild.lastChild.firstChild.nodeValue;
    (XML.details . video .textnode .nodeValue);

    This is because the XML has one root node so firstChild and lastChild are the same, then the lastChild of that node is "video" which also has only a single child node (the text itself) so firstChild and lastChild of the middle element are identical.

    To get to the middle node, you can use one of:

    middleNode = this.firstChild.lastChild.previousSibling.firstChi ld.nodeValue;
    (middleNode = (XML.details.video.instructions.textnode.nodeValue );

    Or:
    middleNode = this.firstChild.firstChild.nextSibling.firstChild. nodeValue;
    (middleNode = (XML.details.product.instructions.textnode.nodeVal ue);

  11. #11
    Member
    Join Date
    Nov 2003
    Posts
    67
    It works, but now i lose my html formatting in my XML document,.

  12. #12
    Senior Member
    Join Date
    Aug 2002
    Location
    Dublin, Ireland
    Posts
    1,749
    That is because the HTML tags would be interpreted as XML and vice versa, so HTML formatting should really be encoded ("&gt;" rather than ">" for example.
    An alternative, is to plonk your HTML direct into the HTML and then set the textfield to the actual textnode, rather than its nodeValue.
    This is fine if you make sure your start and end tags match (which they should for HTML as well, but there are one or two such as "br" where that is not the case).

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