xml学习笔记3--xml的创建
先前由于一直没有注释下面这一行一直报下面的错误。
56 //? ><?php--
google以后根据http://blog.csdn.net/lilien1010/article/details/8793949,还以为是编码的错误。http://abloz.com/2011/10/13/php-sheng-cheng-xml-di-wen-ti.html后来发现原来还是因为出现了多余的空格节点的原因。
PHP语言: 高亮代码由发芽网提供
01 <?php
02 //dom创建一个xml文档
03 //1.创建dom对象
04 $document_xml = new DOMdocument("1.0","utf-8");
05
06 //2.利用dom对象中的方法创建节点和融合节点
07 // 2.1.创建节点
08 // 创建文本节点
09 $text_xml_name = $document_xml->createTextNode("诺基亚");
10 // 创建普通元素节点
11 $name_xml = $document_xml->createElement("name");
12
13 // 创建文本节点
14 $text_xml_price= $document_xml->createTextNode("999");
15 // 创建属性节点
16 $price_xml = $document_xml->createElement("price");
17
18 // 创建文本节点
19 $text_xml_desc = $document_xml->createCDATASection("这是一部好手机");
20 // 创建属性节点
21 $desc_xml = $document_xml->createElement("desc");
22
23
24 // 2.2.融合节点appendChild()
25 $name_xml->appendChild($text_xml_name);
26 $price_xml->appendChild($text_xml_price);
27 $desc_xml->appendChild($text_xml_desc);
28
29 //
30 $goods_xml=$document_xml->createElement("goods");
31 $goods_attr_xml = $document_xml->createAttribute("gid");
32 $goods_attr_xml->value = "1";
33
34 $goods_xml->appendChild($goods_attr_xml);
35 $goods_xml->appendChild($name_xml);
36 $goods_xml->appendChild($price_xml);
37 $goods_xml->appendChild($desc_xml);
38
39 $shop_xml = $document_xml->createElement("shop");
40 $shop_xml->appendChild($goods_xml);
41
42 header("Content-type:text/xml;charset:utf-8;");
43 //错误的写法
44 //echo $document_xml->saveXML();
45 //第一种方法输出
46 //echo $document_xml->saveXML($shop_xml);
47
48 // 第二种方法输出.xml
49 //$document_xml->appendChild($shop_xml);
50 //echo $document_xml->saveXML();
51 //echo $document_xml->saveXML($name_xml);从$name_xml节点开始输出
52 //echo $document_xml->saveXML($document_xml);
53 //echo $document_xml->save("generate_02.xml")?"OK":"NO";
54 //echo $document_xml->save("generate_02.xml");
55
56 //? ><?php--
57
58 $text_xml_name = $document_xml->createTextNode("天龙八部");
59 $name_xml = $document_xml->createElement("name");
60 $text_xml_price= $document_xml->createTextNode("12.3");
61 $price_xml = $document_xml->createElement("price");
62 $text_xml_desc = $document_xml->createCDATASection("这是一本好书,>10000人在读");
63 $desc_xml = $document_xml->createElement("desc");
64
65 $name_xml->appendChild($text_xml_name);
66 $price_xml->appendChild($text_xml_price);
67 $desc_xml->appendChild($text_xml_desc);
68
69 $goods_xml=$document_xml->createElement("goods");
70 $goods_attr_xml = $document_xml->createAttribute("gid");
71 $goods_attr_xml->value = "0";
72
73 $goods_xml->appendChild($goods_attr_xml);
74 $goods_xml->appendChild($name_xml);
75 $goods_xml->appendChild($price_xml);
76 $goods_xml->appendChild($desc_xml);
77
78 //$shop_xml = $document_xml->createElement("shop");
79 $shop_xml->appendChild($goods_xml);
80
81 $document_xml->appendChild($shop_xml);
82
83 echo $document_xml->saveXML($document_xml);
84
85
86 ?>
02 //dom创建一个xml文档
03 //1.创建dom对象
04 $document_xml = new DOMdocument("1.0","utf-8");
05
06 //2.利用dom对象中的方法创建节点和融合节点
07 // 2.1.创建节点
08 // 创建文本节点
09 $text_xml_name = $document_xml->createTextNode("诺基亚");
10 // 创建普通元素节点
11 $name_xml = $document_xml->createElement("name");
12
13 // 创建文本节点
14 $text_xml_price= $document_xml->createTextNode("999");
15 // 创建属性节点
16 $price_xml = $document_xml->createElement("price");
17
18 // 创建文本节点
19 $text_xml_desc = $document_xml->createCDATASection("这是一部好手机");
20 // 创建属性节点
21 $desc_xml = $document_xml->createElement("desc");
22
23
24 // 2.2.融合节点appendChild()
25 $name_xml->appendChild($text_xml_name);
26 $price_xml->appendChild($text_xml_price);
27 $desc_xml->appendChild($text_xml_desc);
28
29 //
30 $goods_xml=$document_xml->createElement("goods");
31 $goods_attr_xml = $document_xml->createAttribute("gid");
32 $goods_attr_xml->value = "1";
33
34 $goods_xml->appendChild($goods_attr_xml);
35 $goods_xml->appendChild($name_xml);
36 $goods_xml->appendChild($price_xml);
37 $goods_xml->appendChild($desc_xml);
38
39 $shop_xml = $document_xml->createElement("shop");
40 $shop_xml->appendChild($goods_xml);
41
42 header("Content-type:text/xml;charset:utf-8;");
43 //错误的写法
44 //echo $document_xml->saveXML();
45 //第一种方法输出
46 //echo $document_xml->saveXML($shop_xml);
47
48 // 第二种方法输出.xml
49 //$document_xml->appendChild($shop_xml);
50 //echo $document_xml->saveXML();
51 //echo $document_xml->saveXML($name_xml);从$name_xml节点开始输出
52 //echo $document_xml->saveXML($document_xml);
53 //echo $document_xml->save("generate_02.xml")?"OK":"NO";
54 //echo $document_xml->save("generate_02.xml");
55
56 //? ><?php--
57
58 $text_xml_name = $document_xml->createTextNode("天龙八部");
59 $name_xml = $document_xml->createElement("name");
60 $text_xml_price= $document_xml->createTextNode("12.3");
61 $price_xml = $document_xml->createElement("price");
62 $text_xml_desc = $document_xml->createCDATASection("这是一本好书,>10000人在读");
63 $desc_xml = $document_xml->createElement("desc");
64
65 $name_xml->appendChild($text_xml_name);
66 $price_xml->appendChild($text_xml_price);
67 $desc_xml->appendChild($text_xml_desc);
68
69 $goods_xml=$document_xml->createElement("goods");
70 $goods_attr_xml = $document_xml->createAttribute("gid");
71 $goods_attr_xml->value = "0";
72
73 $goods_xml->appendChild($goods_attr_xml);
74 $goods_xml->appendChild($name_xml);
75 $goods_xml->appendChild($price_xml);
76 $goods_xml->appendChild($desc_xml);
77
78 //$shop_xml = $document_xml->createElement("shop");
79 $shop_xml->appendChild($goods_xml);
80
81 $document_xml->appendChild($shop_xml);
82
83 echo $document_xml->saveXML($document_xml);
84
85
86 ?>
xml学习笔记3--xml的创建
http://blog.soul11201.com/2014/02/03/56-google-httpblog/