只从xml输出php回显某些xml标签

时间:2017-06-28 05:34:05

标签: php xml

我有这个脚本与数组$result我无法弄清楚为什么我没有显示我想要的标签: 我们假设我有这个XML:

<result>
<domain>google.com</domain>
<update_date>27.6.17</update_date>
</result>

我的php脚本:

    foreach ($result as $values) {

    $domains = file_get_contents("https://www.somedomain.com/api?key=asdasf=$values");
    $xml = new SimpleXMLElement($domains);
    echo $xml->result->domain; 
    echo("<pre>".htmlspecialchars($xml->asXML())."</pre>");
 }

使用此代码,我将显示完整的xml代码...

    foreach ($result as $values) {

    $domains = file_get_contents("https://www.somedomain.com/api?key=asdasf=$values");
    echo $domains;
 }

1 个答案:

答案 0 :(得分:0)

作为如何使用DOMDocument而不是SimpleXML(我不使用)处理xml文档的示例,以下可能会给您一些想法。

<!--minbooks.xml-->

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

/* 
    Rather than read the file first you can do $dom->load( $url )
    For the sake of the demo - consider $xml to be the return value 
    from your `file_get_contents` call
*/
$xml=file_get_contents( realpath( 'C:\wwwroot\xml\minbooks.xml' ) );

$dom=new DOMDocument;
$dom->loadXML( $xml );

/* Find whatever nodes of interest */
$books = $dom->getElementsByTagName('book');

/* process the collection */
foreach( $books as $book ){

    $author = $book->childNodes->item(1);
    $title = $book->childNodes->item(3);

    echo $author->tagName . ' '.$author->nodeValue . ' '.$title->tagName .' '.$title->nodeValue . '<br />';
}


Output
------

author Gambardella, Matthew title XML Developer's Guide
author Ralls, Kim title Midnight Rain
author Galos, Mike title Visual Studio 7: A Comprehensive Guide

使用您发布的xml的代表,更正错误的结束标记,这可能会有所帮助。

$xml='<?xml version="1.0"?>
        <results>
            <result>
                <domain>google.com</domain>
                <update_date>27.6.17</update_date>
            </result>
            <result>
                <domain>froogle.com</domain>
                <update_date>26.6.17</update_date>
            </result>
            <result>
                <domain>droogle.com</domain>
                <update_date>25.6.17</update_date>
            </result>
        </results>';


libxml_use_internal_errors( true ) ;
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->standalone=true;
$dom->strictErrorChecking=false;
$dom->recover=true;

$dom->loadXML( $xml );

$errs=libxml_get_errors();
libxml_clear_errors();



$domains = $dom->getElementsByTagName('domain');
foreach( $domains as $domain ){
    echo $domain->nodeValue . '<br />';
}