代码如下 | 复制代码 |
$xml_str = "<?xml version='1.0'?>"; $xml_str .= "<books>"; $xml_str .= "<book>"; $xml_str .= "<title>harry potter</title>"; $xml_str .= "<author>j.k.rowling</author>"; $xml_str .= "<publisher>warner bros.</publisher>"; $xml_str .= "<price>39.0</price>"; $xml_str .= "</book>"; $xml_str .= "</books>"; $dom = new domdocument; echo $dom->savexml(); //domdocument 加载xml文档 $dom = new domdocument; $dom->load('17-1.xml'); |
//解析xml文档
代码如下 | 复制代码 |
$doc = new domdocument(); $doc->load("17-1.xml"); $books = $doc->getelementsbytagname("book"); |
/*
17-1.xml
代码如下 | 复制代码 |
<?xml version="1.0" encoding="gb2312"?> <books> <book> <title>learning php5</title> <author>david</author> <publisher>white water press</publisher> <price>29.90</price> </book> <book> <title>learning xml</title> <author>jeffson</author> <publisher>white water press</publisher> <price>50.79</price> </book> <book> <title>using perl</title> <author>lucas</author> <publisher>white water press</publisher> <price>17.00</price> </book> <book> <title>windows networks</title> <author>paul</author> <publisher>white water press</publisher> <price>32.99</price> </book> <book> <title>fly leaf</title> <author>jenny</author> <publisher>white water press</publisher> <price>19.50</price> </book> </books> |
*/
?>