【创建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
php cookbook
fsl
trac
【xml文件处理方法-xpath】
$s=simplexml_load_file(12-1.xml);
$ids=$s->xpath(/people/person/@id);//处理属性
foreach ($ids as $id)
{
echo $id.
;
}
$pname=$s->xpath(/people/person/name);//处理节点
foreach ($pname as $name)
{
echo $name.
;
}
$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.
;
}
$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.
;
}
$dname=$xpath->query(/people/person/age);
foreach ($dname as $name)
{
echo $name->nodevalue.
;
}
$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.
;
echo $persion->nodevalue.
;
}
12-1.xml
张俊
20
河北
石家庄
人民东路
13#
刘军
21
吉林
长春
解放南路
25#
周泰
26
广东
深圳
深南路
37#
张群
20
广西
南宁
江南大道
67#
7-2.xsl
【应用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);