PHP读取XML 值

WBOY
WBOYoriginal
2016-06-23 14:29:58729parcourir

Php读取XML第三种情况:XML文件(cy.xml)如下:<?xml version="1.0" encoding="gb2312"?><xml><main><list>1</list><list>2</list><list>3</list></main><main><list>4</list><list>5</list><list>6</list></main></xml>Php文件(cy.php)如下(和第一次依然没有变化):<?php$xml = new DOMDocument();$xml->load('cy.xml');foreach($xml->getElementsByTagName_r('list') as $list){$value = $list->firstChild->nodeValue;echo $value."<br />";}?>cy.php的输出结果:123456===========Php读取XML第四种情况,保持cy.xml不变,改变cy.php:XML文件(cy.xml)如下:<?xml version="1.0" encoding="gb2312"?><xml><main><list>1</list><list>2</list><list>3</list></main><main><list>4</list><list>5</list><list>6</list></main></xml>Php文件(cy.php)如下:<?php$xml = new DOMDocument();$xml->load('cy.xml');$main = $xml->getElementsByTagName_r('main');foreach( $main as $main){$list = $main->getElementsByTagName_r( "list" );foreach ( $list as $list ){$value = $list->firstChild->nodeValue;  Php读取XML第三种情况:XML文件(cy.xml)如下:<?xml version="1.0" encoding="gb2312"?><xml><main><list>1</list><list>2</list><list>3</list></main><main><list>4</list><list>5</list><list>6</list></main></xml>Php文件(cy.php)如下(和第一次依然没有变化):<?php$xml = new DOMDocument();$xml->load('cy.xml');foreach($xml->getElementsByTagName_r('list') as $list){$value = $list->firstChild->nodeValue;echo $value."<br />";}?>cy.php的输出结果:123456===========Php读取XML第四种情况,保持cy.xml不变,改变cy.php:XML文件(cy.xml)如下:<?xml version="1.0" encoding="gb2312"?><xml><main><list>1</list><list>2</list><list>3</list></main><main><list>4</list><list>5</list><list>6</list></main></xml>Php文件(cy.php)如下:<?php$xml = new DOMDocument();$xml->load('cy.xml');$main = $xml->getElementsByTagName_r('main');foreach( $main as $main){$list = $main->getElementsByTagName_r( "list" );foreach ( $list as $list ){$value = $list->firstChild->nodeValue;  echo $value."<br />";}}?>cy.php输出结果:123456为什么两次的cy.php不同,但输出结果是相同的?让我们来看下一个例子==============Php读取XML第五种情况,改变cy.xml,使cy.php保持第四种情况:XML文件(cy.xml)如下:<?xml version="1.0" encoding="gb2312"?><xml><main><list>1</list><list>2</list><list>3</list></main><main><list>4</list><list>5</list><list>6</list></main><m><list>7</list><list>8</list><list>9</list></m></xml>Php文件(cy.php)和第四种情况相同:<?php$xml = new DOMDocument();$xml->load('cy.xml');$main = $xml->getElementsByTagName_r('main');foreach( $main as $main){$list = $main->getElementsByTagName_r( "list" );foreach ( $list as $list ){$value = $list->firstChild->nodeValue;echo $value."<br />";}}?>cy.php输出结果为:123456为什么<m><list>7</list><list>8</list><list>9</list></m>中的7,8,9没有被读出来?因为我们的cy.php只读取<main></main>标记中的内容.<m></m>标记中的内容是不会被读取的.我们这里所提到的"标记",在XML里被称做"节点";注:$xml->load('cy.xml');加载XML文件$xml->loadXML('cy.xml');加载XML格式的字符串

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:[php]php设计模式 (总结)Article suivant:PHP 5.3 与 Zend编译