Results 1 to 13 of 13

Thread: [RESOLVED] xml in html (php)

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [RESOLVED] xml in html (php)

    Hi,

    I have used xml in flash and figured out how accessing the button names work. This is the code I used:

    Quote Originally Posted by Nightwalker83 View Post
    I figured out the solution!

    Code:
    var req: URLRequest = new URLRequest("menuDef.xml");
    var loader:URLLoader = new URLLoader(req);
    var i:int
    var myXML:XML = new XML();
    loader.load(req);
    
    
    loader.addEventListener(Event.COMPLETE,dataLoaded);
    //Button code
    function dataLoaded (event: Event){
    	myXML = new XML(loader.data);
    	for (i = 0; i < 5; i++){
    		var j:int = i+ 1; 
    		/**3 is the line in the  
    		xml the labels start from. (i) starts counting from the 4th line until the end
            specified above.**/		
    		this["btnMenu"+ j].btText.text = myXML.child(3).child(i);
    	}      
    }
    If the items in your xml file are more or less than "5" change the number to suit.
    Now! My question is how would I access the xml data via html code and use it to display the button name flash or html?

    At the moment I am using this code within each flash button:

    Code:
    var req: URLRequest = new URLRequest("../xml/menuDef.xml"); //Remove the "/" before the folder name for the splash page.
    var loader:URLLoader = new URLLoader(req);
    var i:int
    var myXML:XML = new XML();
    this.addEventListener(MouseEvent.CLICK,callurl);
    
    loader.load(req);
    
    loader.addEventListener(Event.COMPLETE,dataLoaded);
    //Button code
    function dataLoaded (event: Event){
    	myXML = new XML(loader.data);
    	for (i = 0; i < 1; i++){
    		var j:int = i+ 1; 
    		/**3 is the line in the  
    		xml the labels start from. (i) starts counting from the 4th line until the end
            specified above.**/		
    		this["btn"+ j].btText.text = myXML.child(3).child(i);
    	}
    }
    However, I doubt that is wise pragmatically since I repeat the code in every button. In flash I could just put all the button code such as above in the main page that would be it. Is this possible to via html?

    Thanks,


    Nightwalker
    Last edited by Nightwalker83; Nov 7th, 2009 at 06:27 PM. Reason: Adding more
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: xml in html

    I'm a little confused as to what you're trying to accomplish, but it sounds like you have some data in an XML file that you want to apply to multiple (button) elements on an HTML page? If you can, perhaps post a portion of your XML (with the target data showing), and a mock up of the HTML you'd like to have as a result after parsing the XML.

    In any case, you can't parse XML with HTML alone; your options are likely PHP or Javascript. Javascript can be turned off, so if the data you're fetching from XML is crucial to your page, you'll be using PHP.

  3. #3

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: xml in html

    Quote Originally Posted by SambaNeko View Post
    I'm a little confused as to what you're trying to accomplish, but it sounds like you have some data in an XML file that you want to apply to multiple (button) elements on an HTML page? If you can, perhaps post a portion of your XML (with the target data showing), and a mock up of the HTML you'd like to have as a result after parsing the XML.
    What I'm trying to do in this case is the same as I did above. That is change the text of the buttons via the xml and code.

    In any case, you can't parse XML with HTML alone; your options are likely PHP or Javascript. Javascript can be turned off, so if the data you're fetching from XML is crucial to your page, you'll be using PHP.
    Ah ok! How would I do that?

    This is an example of the xml I am using:

    Code:
    <?xml version="1.0"?>
    <menu>
    <font>verdana</font>
    <color>0x009900</color>
    <size>20</size>
    <menuLabels>
    <label>Home</label>
    <label>Products</label>
    <label>Customers</label>
    <label>Email Admin</label>
    <label>About Us</label>
    <label>Admin Login</label>
    </menuLabels>
    <content>
    <label>Contact Us</label>
    <label>Submit Email</label>
    <label>Find Product</label>
    <label>Submit</label>
    <label>Register</label>
    <label>Log in</label>
    <label>Log out</label>
    <label>Enter</label>
    </content>
    </menu>
    Edit:

    I will also be using the noscript tags and hyperlinks when javascript is disabled so the javascript method is not a problem.
    Last edited by Nightwalker83; Nov 6th, 2009 at 08:23 PM. Reason: Adding more
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: xml in html

    If you want to find a more in-depth tutorial on the subject, Google "php xml parse", but the basic premise is that you need to create a parser in PHP, and then tell the parser what to do when it encounters elements in your XML.

    Creating the parser is easy:
    Code:
    $xml_parser = xml_parser_create();
    Now tell the parser what to do with elements and character data:
    Code:
    xml_set_element_handler($xml_parser, "startTag", "endTag");
    xml_set_character_data_handler($xml_parser, "contents");
    xml_set_element_handler() assigns a function that the parser should call when it encounters an opening tag, and a function it should call for a closing tag. You can name these functions whatever you want. xml_set_character_data_handler() is similar, but it handles what the parser does with the data between the tags.

    Now define those functions:
    Code:
    function startTag($parser,$name,$attrib){
    }
    function endTag($parser,$name){
    }
    function contents($parser,$data){
    }
    Each function must receive specific arguments - a reference to the parser, the name of the tag or the data of the element, and for the opening tag function, an array of attributes on the tag. Now you can write these functions to do whatever you want. For your case, you might want to grab the relevant data and store in an array for later use. So that might go something like...

    Code:
    $menuLabels = array();
    $inMenu = 0;
    
    function startTag($parser,$name,$attrib){
      if($name == "MENULABELS"){
        $GLOBALS['inMenu'] = 1;
      }
    }
    function endTag($parser,$name){
      if($name == "MENULABELS"){
        $GLOBALS['inMenu'] = 0;
      }  
    }
    function contents($parser,$data){
      if($GLOBALS['inMenu']){
        global $menuLabels;
        array_push($menuLabels,$data);
      }
    }
    The $inMenu variable is simply to set whether or not we're within the "menuLabels" element, because that's where the relevant data is. When $inMenu is true (1), we want to grab the contents of every sub-element, which the contents() function does.

    With everything set up, now you need to get the XML and run it through the parser. That looks like this:
    Code:
    $fp = fopen("example.xml", "r");
    $data = str_replace(array("\r", "\n", "\t"),"",fread($fp, 80000));
    
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
    }
    
    xml_parser_free($xml_parser);
    fclose($fp);
    Your final code looks like this:
    Code:
    <?php
    $menuLabels = array();
    $inMenu = 0;
    
    function startTag($parser,$name,$attrib){
      if($name == "MENULABELS"){
        $GLOBALS['inMenu'] = 1;
      }
    }
    function endTag($parser,$name){
      if($name == "MENULABELS"){
        $GLOBALS['inMenu'] = 0;
      }  
    }
    function contents($parser,$data){
      if($GLOBALS['inMenu']){
        global $menuLabels;
        array_push($menuLabels,$data);
      }
    }
    
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startTag", "endTag");
    xml_set_character_data_handler($xml_parser, "contents");
    
    $fp = fopen("example.xml", "r");
    $data = str_replace(array("\r", "\n", "\t"),"",fread($fp, 80000));
    
    if(!(xml_parse($xml_parser, $data, feof($fp)))){
        die("Error on line " . xml_get_current_line_number($xml_parser));
    }
    
    xml_parser_free($xml_parser);
    fclose($fp);
    
    print_r($menuLabels);
    ?>
    The print_r($menuLabels) at the end should print out an array with each of your menuLabels label elements. Once you've got that, you can then use the array to populate your buttons...

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: xml in html

    Thanks, I give it a try.

    Edit:

    I can't figure out how to tell the button to use the "$menuLabels" value as the button label.

    I have tried:

    PHP Code:
    echo "btn.text = $menuLabels(1)"
    Where btn is the name of the button but it didn't work
    Last edited by Nightwalker83; Nov 7th, 2009 at 07:05 PM. Reason: Adding more
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  6. #6

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: xml in html (php)

    I've never used PHP and Flash like this, but I would assume that the stuff you're taking in from PHP is stored as a string in Flash. you would need to take the "$menuLabels[1]" string and store it in Flash, not an entire statement (unless you can then execute the entire string as a statement). and you're using regular brackets there instead of squared brackets, which would do nothing if you were to echo it out. so. yeah.

  8. #8
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: xml in html (php)

    I'm kind of confused again...

    I thought the goal here was to get data from XML, to be used in HTML. Why are we trying to send $menuLabels back into Flash? There's little point in having PHP do the parsing to send the resulting data to Flash...

  9. #9

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: xml in html (php)

    Quote Originally Posted by SambaNeko View Post
    I'm kind of confused again...

    I thought the goal here was to get data from XML, to be used in HTML. Why are we trying to send $menuLabels back into Flash? There's little point in having PHP do the parsing to send the resulting data to Flash...
    No! What I was asking for in my first post was how to populate the flash button text with the xml text via php. Sorry, if I confused you but because I had never dealt with php/xml before.

    Basically it is:

    Flash Button with text not set can I set it via xml using php?

    So if the button text = "" can I make it equal = "Hello" (Hello being the value in the xml file) using php.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: xml in html (php)

    this really has nothing to do with PHP, and everything to do with Flash. I'd try asking in a forum to do with that instead. you've already done the part that involves PHP. you're interested in setting Flash button text, though, and I'm not sure if anyone here can help you all that much.

    I bet ActionScript could have parsed the XML file, too. you might want to try looking into just opening an XML file within Flash and taking stuff from it, instead of trying to use PHP as a middle-man.

    either way, the PHP part of this equation is finished.

  11. #11

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: xml in html (php)

    Quote Originally Posted by kows View Post
    this really has nothing to do with PHP, and everything to do with Flash. I'd try asking in a forum to do with that instead. you've already done the part that involves PHP. you're interested in setting Flash button text, though, and I'm not sure if anyone here can help you all that much.
    So, I would be better off putting the original posts code in every single button? Although, maybe I can get the code to via via an external action script file.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  12. #12
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: xml in html (php)

    Put the code in your original post on a frame in your timeline after (or at the same time as) your buttons exist on the stage. You may need to modify this one line...
    Code:
    this["btn"+ j].btText.text = myXML.child(3).child(i);
    ...to correctly target your buttons.

    Sorry, if I'd understood what you wanted I would've suggested that from the beginning - but now you know about parsing XML in PHP, which is good too.

    Edit: and if you do want to import variables to Flash from HTML, you can use FlashVars, like this...
    Code:
    <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" id="Untitled-1" align="middle">
    <param name="allowScriptAccess" value="sameDomain" />
    <param name="FlashVars" value="myText=hi" />
    <param name="allowFullScreen" value="false" />
    <param name="movie" value="Untitled-1.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />
    <embed FlashVars="myText=hi" src="Untitled-1.swf" quality="high" bgcolor="#ffffff" width="550" height="400" name="Untitled-1" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
    </object>
    You put your variables in a string and then assign them to the FlashVars parameter (use it in both the param tag and the embed tag for full browser coverage). So, if you were using the PHP/XML method, you'd need to assemble a string of your vars ("btn1=$menuLabels[0]&btn2=$menuLabels[1]&btn3= ... " etc.) and put that in FlashVars. Then in Flash you access them through LoaderInfo...
    Code:
    var params:Object = LoaderInfo(this.root.loaderInfo).parameters;
    btn.text = params.btn1;  // = the value of $menuLabels[0]
    Last edited by SambaNeko; Nov 8th, 2009 at 07:43 PM.

  13. #13

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: xml in html (php)

    It was my fault! I assumed you understood what I was talking about in my original post. From what I can see I have 2 other options try and use an external action script file to do what I need or apply the text for the buttons manually.

    Edit:

    I entered the button text manually instead. For some strange reason the flash code wouldn't populate the buttons with the xml data when the code was used within the buttons themselves.
    Last edited by Nightwalker83; Nov 9th, 2009 at 05:22 PM. Reason: Adding more
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

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