> php教程 > php手册 > 본문

PHP5中的SimpleXML解析XML文档

WBOY
풀어 주다: 2016-06-06 19:56:30
원래의
1232명이 탐색했습니다.

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 1、访问具有相同元素名称的节点 2、通过foreach循环所有相同元素名称的子节点 以下为引用的内容: foreach($xml-channel-item as $key=$value){ print “Title: ” . $item-title . “\n”; } 3、输出

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

 

    1、访问具有相同元素名称的节点

    2、通过foreach循环所有相同元素名称的子节点

    以下为引用的内容:

    foreach($xml->channel->item as $key=>$value){

    print “Title: ” . $item->title . “\n”;

    }

    3、输出整个文档

    echo $xml->asXML();

    4、把节点作为字符串输出

    echo $xml->channel->item[0]->asXML();

    这将输出文本

    以下为引用的内容:

   

   

Braised Sea Cucumber

    //menu.example./dishes.php?dish=cuke

    Gentle flavors of the sea that nourish and refresh you.

   

    带文件名参数的asXML将会把原本输出的内容保存为一个文件

    $xml->channel->item[0]->asXML(“item[0].xml”);

    完整的代码:

    以下为引用的内容:

    rss.xml

   

   

   

   

What's For Dinner

    menu.example./

    These are your choices of what to eat tonight.

   

   

Braised Sea Cucumber

    menu.example./dishes.php?dish=cuke

    Gentle flavors of the sea that nourish and refresh you.

   

   

   

Baked Giblets with Salt

    menu.example./dishes.php?dish=giblets

    Rich giblet flavor infused with salt and spice.

   

   

   

Abalone with Marrow and Duck Feet

    menu.example./dishes.php?dish=abalone

    There's no mistaking the special pleasure of abalone.

   

   

   

    rss.php文件内容如下:

   

    $xml = simplexml_load_file(“rss.xml”);

    echo “

”.$xml->channel->title.“


”;

    echo “

    ”;

        echo “

  • Title:”.$xml->channel->item[0]->title.“
  • ”;

        echo “

  • Title:”.$xml->channel->item[1]->title.“
  • ”;

        echo “

  • Title:”.$xml->channel->item[2]->title.“
  • ”;

        echo “

”;

    print “Title: ” . $xml->channel->item[0]->title . “\n
”;

    print “Title: ” . $xml->channel->item[1]->title . “\n
”;

    print “Title: ” . $xml->channel->item[2]->title . “\n
”;

    echo “


”;

    foreach ($xml->channel->item[0] as $element_name => $content) {

    print “The $element_name is $content\n
”;

    }

    echo “


”;

    print_r($xml);

    echo $xml->channel->item[0]->asXML();

    ?>

    任何XML文本在输出前最好用 htmlentiteis() 函数编码后再输出,否这可能出现问题

  [1] [2] 

PHP5中的SimpleXML解析XML文档

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!