• php学习——xml


    【创建xml】

    $dom=new DOMDocument("1.0");
    $book=$dom->appendChild($dom->createElement('book'));//创建根元素
    $title=$book->appendChild($dom->createElement('title'));//在根元素下创建节点
    $title->appendChild($dom->createTextNode('php cookbook'));//在节点下创建子节点
    $title->setAttribute("cover","soft");//title节点下创建属性

    $sklar=$book->appendChild($dom->createElement('author'));
    //$sklar->appendChild($dom->createTextNode('sklar'));

    $name=$sklar->appendChild($dom->createElement('name'));
    $name->appendChild($dom->createTextNode('fsl'));

    $trac=$book->appendChild($dom->createElement('author'));
    $trac->setAttribute("cover","feng");
    $trac->setAttribute("id","123");
    $trac->appendChild($dom->createTextNode('trac'));

    $dom->formatOutput=true;
    echo $dom->saveXML();
    $dom->save('test.xml');
    $dom->saveHTMLFile('1.html');

    1.htm

    <?xml version="1.0"?>
    <book>
    <title cover="soft">php cookbook</title>
    <author>
    <name>fsl</name>
    </author>
    <author cover="feng" id="123">trac</author>
    </book>

    【xml文件处理方法-xpath】


    $s=simplexml_load_file("12-1.xml");

    $ids=$s->xpath("/people/person/@id");//处理属性
    foreach ($ids as $id)
    {
    echo $id."<br>";
    }

    $pname=$s->xpath("/people/person/name");//处理节点

    foreach ($pname as $name)
    {
    echo $name."<br>";
    }

    $per=$s->xpath("/people/person");
    foreach ($per as $person){
    list($age)=$person->xpath("age");
    list($city)=$person->xpath("address/city");
    list($pro)=$person->xpath("address/province");//处理含有子节点的节点

    echo $city."_".$pro.":".$age."<br>";
    }

    $dom=new DOMDocument;
    $dom->load("12-1.xml");
    $xpath=new DOMXPath($dom);
    $did=$xpath->query("/people/person/@id");

    foreach ($did as $id)
    {
    echo $id->nodeValue."<br>";
    }

    $dname=$xpath->query("/people/person/age");

    foreach ($dname as $name)
    {
    echo $name->nodeValue."<br>";
    }


    $dper=$xpath->query("/people/person");

    foreach ($dper as $persion)
    {
    $fn=$xpath->query('name',$persion);
    echo $fn->item(0)->firstChild->nodeValue." : ";

    $fn2=$xpath->query('age',$persion);
    echo $fn2->item(0)->firstChild->nodeValue."<br>";

    echo $persion->nodeValue."<br>";
    }

    12-1.xml

    <?xml version="1.0"?>
    <?xml-stylesheet href="7-2.xsl" type="text/xsl"?>
    <people>
    <person id="001">
    <name>张俊</name>
    <age>20</age>
    <address>
    <province>河北</province>
    <city>石家庄</city>
    <street>人民东路</street>
    <house_number>13#</house_number>
    </address>
    </person>
    <person id="002">
    <name>刘军</name>
    <age>21</age>
    <address>
    <province>吉林</province>
    <city>长春</city>
    <street>解放南路</street>
    <house_number>25#</house_number>
    </address>
    </person>
    <person id="003">
    <name>周泰</name>
    <age>26</age>
    <address>
    <province>广东</province>
    <city>深圳</city>
    <street>深南路</street>
    <house_number>37#</house_number>
    </address>
    </person>
    <person id="004">
    <name>张群</name>
    <age>20</age>
    <address>
    <province>广西</province>
    <city>南宁</city>
    <street>江南大道</street>
    <house_number>67#</house_number>
    </address>
    </person>
    </people>

    7-2.xsl

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <xsl:value-of select="people/person/name"/>
    </xsl:template>
    </xsl:stylesheet>


    【应用xslt】

    必须打开php扩展php_xsl

    $xsl=new DOMDocument;
    $xsl->load('7-2.xsl');

    $xslt=new XSLTProcessor();
    $xslt->importStylesheet($xsl);

    $xml=new DOMDocument;
    $xml->load('12-1.xml');//双参数问题

    $results=$xslt->transformToXml($xml);

    $results2=$xslt->transformToURI($xml,'results.html');

    $results3=$xslt->transformToDoc($xml);


  • 相关阅读:
    理解z-index
    别在最好的年纪辜负最好的自己
    一劳永逸的flex布局
    移动端常见的兼容性问题
    css代码颜色十六进制大全
    把select的小图标替换成 自己想要的图片
    HTML中的<select>标签如何设置默认选中的选项
    js获取元素,窗口的宽度、高度
    jQuery.NiceScroll简单的API
    Linux搭建ftp服务器简单教程及使用方法
  • 原文地址:https://www.cnblogs.com/fslnet/p/2434825.html
Copyright © 2020-2023  润新知