PHP读取XML 值

WBOY
WBOYOriginal
2016-06-23 14:29:58734browse

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格式的字符串

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:[php]php设计模式 (总结)Next article:PHP 5.3 与 Zend编译