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的输出结果: 1 2 3 4 5 6 =========== 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的输出结果: 1 2 3 4 5 6 =========== 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输出结果: 1 2 3 4 5 6 为什么两次的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输出结果为: 1 2 3 4 5 6 为什么 <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格式的字符串