php學習——xml

來源:互聯網
上載者:User

【建立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);


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.