Get XML element node?

Discussion in 'PHP' started by altergothen, Nov 5, 2008.

  1. #1
    Hi there

    How do I extract the value of a specific XML element node?
    I am trying to extract the <description>value where the title = <title>AUD/USD</title>

    ------------------
    CODE
    ------------------
    # Load the XML file
    $request_url = "localhost/test/rss.xml";
    $xml = simplexml_load_file($request_url) or die("feed not loading");

    # Access Attributes
    ??????????

    ------------------
    XML FILE
    ------------------
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/css" href="../css/rss.css" ?>
    <rss version="2.0">

    <channel>
    <title>Exchange Rates for United States Dollar</title>
    <link>http://xe.com/USD/</link>
    <description>The latest currency exchange rates for United States Dollar</description>
    <lastBuildDate>Wed, 05 Nov 2008 21:25:10 GMT</lastBuildDate>
    <language>en-us</language>
    <copyright>Copyright: (C) The Money Converter, see http://xe.com/ </copyright>
    <docs>http://xe.com/RSSFeeds.aspx</docs>
    <ttl>15</ttl>
    <image>
    <title>The Money Converter Exchange Rates</title>
    <url>http://xe.com/images/artwork/sml.jpg</url>
    <link>http://xe.com/</link>
    </image>

    <item>
    <title>AUD/USD</title>
    <link>http://xe.com/USD/AUD.aspx</link>
    <guid>http://xe.com/USD/AUD.aspx</guid>
    <pubDate>Wed, 05 Nov 2008 21:25:10 GMT</pubDate>
    <description>1 United States Dollar = 1.45996 Australian Dollar</description>
    <category>Oceania</category>
    </item>

    <item>
    <title>CAD/USD</title>
    <link>http://xe.com/USD/CAD.aspx</link>
    <guid>http://xe.com/USD/CAD.aspx</guid>
    <pubDate>Wed, 05 Nov 2008 21:25:10 GMT</pubDate>
    <description>1 United States Dollar = 1.16616 Canadian Dollar</description>
    <category>North America</category>
    </item>


    Thank You :)
     
    altergothen, Nov 5, 2008 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    It would be something like this

    
    
    $contents = file_get_contents("localhost/test/rss.xml");
    $data = array();
    $stuffs= array();
    $inside  = "false";
    $parser = xml_parser_create();
    
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, $contents, $data);
    xml_parser_free($parser);
    
    foreach($data as $datapoint) {
    	
    	if ($datapoint['tag'] == "item" && $datapoint['type'] == "open") {
    		$stuff = new stuff();
    		$inside = "true";
    	}
    
    	if ($datapoint['tag'] == "item" && $datapoint['type'] == "close") {
    		if (!isset($stuffs[$stuff ->id])) {
    			$stuffs[$stuff ->id]=$stuff ;
    		}
    		unset($stuff);	
    		$inside  = "false";
    	}
    	
    	if (isset($stuff) && isset($datapoint['value'])) {		
    		if($datapoint['tag'] == "Title") 		$stuff ->title=$datapoint['value'];
    		if($datapoint['tag'] == "Description") 	$stuff ->description=$datapoint['value'];
    	}
    
    }
    
    class stuff
    {
    	var $title="";
    	var $description="";
    	
    }
    
    
    Code (markup):
     
    javaongsan, Nov 5, 2008 IP