zoukankan      html  css  js  c++  java
  • PHP读取XML 值 PHP读取XML 内容

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

  • 相关阅读:
    Outline of the research(updated 8th,Aug)
    Similarity metrics(Updated Aug,8th)
    Experiments on the NYC dataset(updated 7th,Aug)
    Fused Matrix Factorization & some personal thoughts (Updated 1st,Aug,afternoon)
    Java——图片读取与存储
    Java——setBounds的用法以及居中的方式
    linux 常用命令整理----解压缩
    周一01.4安装PyCharm步骤
    周一01.2 计算机硬件&操作系统
    周一01.3Python多版本共存&pip环境变量设置
  • 原文地址:https://www.cnblogs.com/jayleke/p/1851941.html
Copyright © 2011-2022 走看看