聪明的唯一 發表於 2019-8-3 11:48:00

PHP中的XML解析的5种方法

<p>【前言】<br>&nbsp; &nbsp;不管是桌面软件开发,还是WEB应用,XML无处不在!<br>&nbsp; &nbsp;然而在平时的工作中,仅仅是使用一些已经封装好的类对XML对于处理,包括生成,解析等。假期有空,于是将PHP中的几种XML解析方法总结如下:</p>
<p>&nbsp; &nbsp;以解析Google API 接口提供的天气情况为例,我们取今天的天气及气温。<br>&nbsp; &nbsp;API地址:<span style="color: rgba(255, 102, 0, 1)">http://www.google.com/ig/api?weather=shenzhen</span></p>
<p>【XML文件内容】</p>
<div class="cnblogs_code">
<pre>&lt;?xml version="1.0"?&gt;
&lt;xml_api_reply version="1"&gt;
    &lt;weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0" &gt;
      &lt;forecast_information&gt;
            &lt;city data="Shenzhen, Guangdong"/&gt;
            &lt;postal_code data="shenzhen"/&gt;
            &lt;latitude_e6 data=""/&gt;
            &lt;longitude_e6 data=""/&gt;
            &lt;forecast_date data="2009-10-05"/&gt;
            &lt;current_date_time data="2009-10-04 05:02:00 +0000"/&gt;
            &lt;unit_system data="US"/&gt;
      &lt;/forecast_information&gt;
      &lt;current_conditions&gt;
            &lt;condition data="Sunny"/&gt;
            &lt;temp_f data="88"/&gt;
            &lt;temp_c data="31"/&gt;
            &lt;humidity data="Humidity: 49%"/&gt;
            &lt;icon data="/ig/images/weather/sunny.gif"/&gt;
            &lt;wind_condition data="Wind:mph"/&gt;
      &lt;/current_conditions&gt;
    &lt;/weather&gt;
&lt;/xml_api_reply&gt;</pre>
</div>
<p>【使用DomDocument解析】</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> = get_utf8_string(<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$dom</span> = DOMDocument::loadXML(<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
此处也可使用如下所示的代码,
$dom = new DOMDocument();
$dom-&gt;load($url);
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(128, 0, 128, 1)">$elements</span> = <span style="color: rgba(128, 0, 128, 1)">$dom</span>-&gt;getElementsByTagName("current_conditions"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$element</span> = <span style="color: rgba(128, 0, 128, 1)">$elements</span>-&gt;item(0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "condition"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "temp_c"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$condition</span>, '&lt;br /&gt;'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$temp_c</span>, '&lt;br /&gt;'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">function</span> get_utf8_string(<span style="color: rgba(128, 0, 128, 1)">$content</span>) {    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">将一些字符转化成utf8格式</span>
    <span style="color: rgba(128, 0, 128, 1)">$encoding</span> = mb_detect_encoding(<span style="color: rgba(128, 0, 128, 1)">$content</span>, <span style="color: rgba(0, 0, 255, 1)">array</span>('ASCII','UTF-8','GB2312','GBK','BIG5'<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 0, 255, 1)">return</span>mb_convert_encoding(<span style="color: rgba(128, 0, 128, 1)">$content</span>, 'utf-8', <span style="color: rgba(128, 0, 128, 1)">$encoding</span><span style="color: rgba(0, 0, 0, 1)">);
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span> get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, <span style="color: rgba(128, 0, 128, 1)">$tagname</span><span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(128, 0, 128, 1)">$tags</span> = <span style="color: rgba(128, 0, 128, 1)">$element</span>-&gt;getElementsByTagName(<span style="color: rgba(128, 0, 128, 1)">$tagname</span>);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取得所有的$tagname</span>

    <span style="color: rgba(128, 0, 128, 1)">$tag</span> = <span style="color: rgba(128, 0, 128, 1)">$tags</span>-&gt;item(0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取第一个以$tagname命名的标签</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$tag</span>-&gt;hasAttributes()) {    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取data属性</span>
      <span style="color: rgba(128, 0, 128, 1)">$attribute</span> = <span style="color: rgba(128, 0, 128, 1)">$tag</span>-&gt;getAttribute("data"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$attribute</span><span style="color: rgba(0, 0, 0, 1)">;
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    }
}
</span>?&gt;</pre>
</div>
<p>&nbsp; &nbsp;这只是一个简单的示例,仅包括了loadXML, item, getAttribute,getElementsByTagName等方法,还有一些有用的方法,这个依据你的实际需要。</p>
<p>【XMLReader】<br>&nbsp; &nbsp;当我们要用php解读xml的内容时,有很多物件提供函式,让我们不用一个一个字元去解析,而只要根据标签和属性名称,就能取出文件中的属性与内容了,相较之下方便许多。其中XMLReader循序地浏览过xml档案的节点,可以想像成游标走过整份文件的节点,并抓取需要的内容。</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$xml</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> XMLReader();
</span><span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;open(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = ''<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">while</span> (<span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">read()) {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">      echo $xml-&gt;name, "==&gt;", $xml-&gt;depth, "&lt;br&gt;";</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$condition</span>) &amp;&amp; !<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$temp_c</span><span style="color: rgba(0, 0, 0, 1)">)) {
          </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">;
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;name == 'condition' &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$condition</span>)) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取第一个condition</span>
            <span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;getAttribute('data'<span style="color: rgba(0, 0, 0, 1)">);
      }

      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;name == 'temp_c' &amp;&amp; <span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$temp_c</span>)) {    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取第一个temp_c</span>
          <span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;getAttribute('data'<span style="color: rgba(0, 0, 0, 1)">);
      }

      </span><span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">read();
}

</span><span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;<span style="color: rgba(0, 0, 0, 1)">close();
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$condition</span>, '&lt;br /&gt;'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$temp_c</span>, '&lt;br /&gt;';</pre>
</div>
<p>&nbsp; &nbsp;我们只是需要取第一个condition和第一个temp_c,于是遍历所有的节点,将遇到的第一个condition和第一个temp_c写入变量,最后输出。</p>
<p>【DOMXPath】<br>&nbsp; &nbsp;这种方法需要使用DOMDocument对象创建整个文档的结构,</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$dom</span> = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> DOMDocument();
</span><span style="color: rgba(128, 0, 128, 1)">$dom</span>-&gt;load(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(128, 0, 128, 1)">$xpath</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> DOMXPath(<span style="color: rgba(128, 0, 128, 1)">$dom</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$element</span> = <span style="color: rgba(128, 0, 128, 1)">$xpath</span>-&gt;query("/xml_api_reply/weather/current_conditions")-&gt;item(0<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "condition"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, "temp_c"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$condition</span>, '&lt;br /&gt;'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$temp_c</span>, '&lt;br /&gt;'<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">function</span> get_google_xml_data(<span style="color: rgba(128, 0, 128, 1)">$element</span>, <span style="color: rgba(128, 0, 128, 1)">$tagname</span><span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(128, 0, 128, 1)">$tags</span> = <span style="color: rgba(128, 0, 128, 1)">$element</span>-&gt;getElementsByTagName(<span style="color: rgba(128, 0, 128, 1)">$tagname</span>);   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取得所有的$tagname</span>

    <span style="color: rgba(128, 0, 128, 1)">$tag</span> = <span style="color: rgba(128, 0, 128, 1)">$tags</span>-&gt;item(0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取第一个以$tagname命名的标签</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(128, 0, 128, 1)">$tag</span>-&gt;hasAttributes()) {    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取data属性</span>
      <span style="color: rgba(128, 0, 128, 1)">$attribute</span> = <span style="color: rgba(128, 0, 128, 1)">$tag</span>-&gt;getAttribute("data"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$attribute</span><span style="color: rgba(0, 0, 0, 1)">;
    }</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
    }
}
</span>?&gt;</pre>
</div>
<p>【xml_parse_into_struct】<br>&nbsp; &nbsp;说明:int xml_parse_into_struct ( resource parser, string data, array &amp;values [, array &amp;index] )</p>
<p>&nbsp; &nbsp;该函数将 XML 文件解析到两个对应的数组中,index 参数含有指向 values 数组中对应值的指针。最后两个数组参数可由指针传递给函数。<br>&nbsp; &nbsp;注意: xml_parse_into_struct() 失败返回 0,成功返回 1。这和 FALSE 与 TRUE 不同,使用例如 === 的运算符时要注意。</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: rgba(0, 0, 0, 1)">PHP
</span><span style="color: rgba(0, 128, 128, 1)">header</span>("Content-type:text/html; Charset=utf-8"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$url</span> = "http://www.google.com/ig/api?weather=shenzhen"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加载XML内容</span>
<span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>(<span style="color: rgba(128, 0, 128, 1)">$url</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$p</span> = <span style="color: rgba(0, 128, 128, 1)">xml_parser_create</span><span style="color: rgba(0, 0, 0, 1)">();
</span><span style="color: rgba(0, 128, 128, 1)">xml_parse_into_struct</span>(<span style="color: rgba(128, 0, 128, 1)">$p</span>, <span style="color: rgba(128, 0, 128, 1)">$content</span>, <span style="color: rgba(128, 0, 128, 1)">$vals</span>, <span style="color: rgba(128, 0, 128, 1)">$index</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">xml_parser_free</span>(<span style="color: rgba(128, 0, 128, 1)">$p</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '天气:', <span style="color: rgba(128, 0, 128, 1)">$vals</span>[<span style="color: rgba(128, 0, 128, 1)">$index</span>['CONDITION']]['attributes']['DATA'], '&lt;br /&gt;'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> '温度:', <span style="color: rgba(128, 0, 128, 1)">$vals</span>[<span style="color: rgba(128, 0, 128, 1)">$index</span>['TEMP_C']]['attributes']['DATA'], '&lt;br /&gt;';</pre>
</div>
<p>【Simplexml】<br>&nbsp; &nbsp;此方法在PHP5中可用<br>&nbsp; &nbsp;这个在google的官方文档中有相关的例子,如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Charset: utf-8</span><span style="color: rgba(0, 128, 0, 1)">
/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 用php Simplexml 调用google天气预报api,和g官方的例子不一样
* google 官方php domxml 获取google天气预报的例子
* http://www.google.com/tools/toolbar/buttons/intl/zh-CN/apis/howto_guide.html
*
* @copyright Copyright (c) 2008 &lt;cmpan(at)qq.com&gt;
* @license New BSD License
* @version 2008-11-9
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 城市,用城市拼音</span>
<span style="color: rgba(128, 0, 128, 1)">$city</span> = <span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$_GET</span>['city']) ? 'shenzhen' : <span style="color: rgba(128, 0, 128, 1)">$_GET</span>['city'<span style="color: rgba(0, 0, 0, 1)">];
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> = <span style="color: rgba(0, 128, 128, 1)">file_get_contents</span>("http://www.google.com/ig/api?weather=<span style="color: rgba(128, 0, 128, 1)">$city</span>&amp;hl=zh-cn"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> || <span style="color: rgba(0, 0, 255, 1)">die</span>("No such city's data"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$content</span> = mb_convert_encoding(<span style="color: rgba(128, 0, 128, 1)">$content</span>, 'UTF-8', 'GBK'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(128, 0, 128, 1)">$xml</span> = <span style="color: rgba(0, 128, 128, 1)">simplexml_load_string</span>(<span style="color: rgba(128, 0, 128, 1)">$content</span><span style="color: rgba(0, 0, 0, 1)">);

</span><span style="color: rgba(128, 0, 128, 1)">$date</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;weather-&gt;forecast_information-&gt;forecast_date-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$html</span> = <span style="color: rgba(128, 0, 128, 1)">$date</span>. "&lt;br&gt;\r\n"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(128, 0, 128, 1)">$current</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;weather-&gt;<span style="color: rgba(0, 0, 0, 1)">current_conditions;

</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>-&gt;condition-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$temp_c</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>-&gt;temp_c-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$humidity</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>-&gt;humidity-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$icon</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>-&gt;icon-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$wind</span> = <span style="color: rgba(128, 0, 128, 1)">$current</span>-&gt;wind_condition-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();

</span><span style="color: rgba(128, 0, 128, 1)">$condition</span> &amp;&amp; <span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;weather-&gt;forecast_conditions-&gt;condition-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
</span><span style="color: rgba(128, 0, 128, 1)">$icon</span> &amp;&amp; <span style="color: rgba(128, 0, 128, 1)">$icon</span> = <span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;weather-&gt;forecast_conditions-&gt;icon-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();

</span><span style="color: rgba(128, 0, 128, 1)">$html</span>.= "当前: {<span style="color: rgba(128, 0, 128, 1)">$condition</span>}, {<span style="color: rgba(128, 0, 128, 1)">$temp_c</span>}°C,&lt;img src='http://www.google.com/ig{<span style="color: rgba(128, 0, 128, 1)">$icon</span>}'/&gt; {<span style="color: rgba(128, 0, 128, 1)">$humidity</span>} {<span style="color: rgba(128, 0, 128, 1)">$wind</span>} &lt;br /&gt;\r\n"<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 0, 255, 1)">foreach</span>(<span style="color: rgba(128, 0, 128, 1)">$xml</span>-&gt;weather-&gt;forecast_conditions <span style="color: rgba(0, 0, 255, 1)">as</span> <span style="color: rgba(128, 0, 128, 1)">$forecast</span><span style="color: rgba(0, 0, 0, 1)">) {
    </span><span style="color: rgba(128, 0, 128, 1)">$low</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>-&gt;low-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
    </span><span style="color: rgba(128, 0, 128, 1)">$high</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>-&gt;high-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
    </span><span style="color: rgba(128, 0, 128, 1)">$icon</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>-&gt;icon-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
    </span><span style="color: rgba(128, 0, 128, 1)">$condition</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>-&gt;condition-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
    </span><span style="color: rgba(128, 0, 128, 1)">$day_of_week</span> = <span style="color: rgba(128, 0, 128, 1)">$forecast</span>-&gt;day_of_week-&gt;<span style="color: rgba(0, 0, 0, 1)">attributes();
    </span><span style="color: rgba(128, 0, 128, 1)">$html</span>.= "{<span style="color: rgba(128, 0, 128, 1)">$day_of_week</span>} : {<span style="color: rgba(128, 0, 128, 1)">$high</span>} / {<span style="color: rgba(128, 0, 128, 1)">$low</span>} °C, {<span style="color: rgba(128, 0, 128, 1)">$condition</span>} &lt;img src='http://www.google.com/ig{<span style="color: rgba(128, 0, 128, 1)">$icon</span>}' /&gt;&lt;br /&gt;\r\n"<span style="color: rgba(0, 0, 0, 1)">;
}

</span><span style="color: rgba(0, 128, 128, 1)">header</span>('Content-type: text/html; Charset: utf-8'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">print</span> <span style="color: rgba(128, 0, 128, 1)">$html</span><span style="color: rgba(0, 0, 0, 1)">;
</span>?&gt;</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/mzhaox/p/11294317.html
頁: [1]
查看完整版本: PHP中的XML解析的5种方法