Rumah  >  Artikel  >  php教程  >  PHP使用DOM XML操作XML[总结]

PHP使用DOM XML操作XML[总结]

WBOY
WBOYasal
2016-06-06 19:42:15862semak imbas

1、前言 XML树状层次结构鲜明,非常适合作为配置文件。PHP中可以使用DOM XML来操作XML。本文总结一下PHP使用DOM XML创建、添加节点、查询XML文件。 2、使用DOM XML XML中节点分为元素和文本,DOMDocument类型是文档类型,提供了操作元素和文本的成员函数和属

1、前言

  XML树状层次结构鲜明,非常适合作为配置文件。PHP中可以使用DOM XML来操作XML。本文总结一下PHP使用DOM XML创建、添加节点、查询XML文件。

2、使用DOM XML

  XML中节点分为元素和文本,DOMDocument类型是文档类型,提供了操作元素和文本的成员函数和属性。

DOMDocument类如下:

DOMDocument <span>extends</span><span> DOMNode {
</span><span>/*</span><span> Properties </span><span>*/</span><span>
readonly </span><span>public</span> <span>string</span> <span>$actualEncoding</span><span> ;
readonly </span><span>public</span> DOMConfiguration <span>$config</span><span> ;
readonly </span><span>public</span> DOMDocumentType <span>$doctype</span><span> ;
readonly </span><span>public</span> DOMElement <span>$documentElement</span><span> ;
</span><span>public</span> <span>string</span> <span>$documentURI</span><span> ;
</span><span>public</span> <span>string</span> <span>$encoding</span><span> ;
</span><span>public</span> bool <span>$formatOutput</span><span> ;
readonly </span><span>public</span> DOMImplementation <span>$implementation</span><span> ;
</span><span>public</span> bool <span>$preserveWhiteSpace</span> =<span>true</span><span> ;
</span><span>public</span> bool <span>$recover</span><span> ;
</span><span>public</span> bool <span>$resolveExternals</span><span> ;
</span><span>public</span> bool <span>$standalone</span><span> ;
</span><span>public</span> bool <span>$strictErrorChecking</span> =<span>true</span><span> ;
</span><span>public</span> bool <span>$substituteEntities</span><span> ;
</span><span>public</span> bool <span>$validateOnParse</span> =<span>false</span><span> ;
</span><span>public</span> <span>string</span> <span>$version</span><span> ;
readonly </span><span>public</span> <span>string</span> <span>$xmlEncoding</span><span> ;
</span><span>public</span> bool <span>$xmlStandalone</span><span> ;
</span><span>public</span> <span>string</span> <span>$xmlVersion</span><span> ;
</span><span>/*</span><span> Methods </span><span>*/</span><span>
__construct ([ </span><span>string</span> <span>$version</span> [, <span>string</span> <span>$encoding</span><span> ]] )
DOMAttr createAttribute ( </span><span>string</span> <span>$name</span><span> )
DOMAttr createAttributeNS ( </span><span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$qualifiedName</span><span> )
DOMCDATAdiv createCDATAdiv ( </span><span>string</span> <span>$data</span><span> )
DOMComment createComment ( </span><span>string</span> <span>$data</span><span> )
DOMDocumentFragment createDocumentFragment ( void )
DOMElement createElement ( </span><span>string</span> <span>$name</span> [, <span>string</span> <span>$value</span><span> ] )
DOMElement createElementNS ( </span><span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$qualifiedName</span> [, <span>string</span> <span>$value</span><span> ] )
DOMEntityReference createEntityReference ( </span><span>string</span> <span>$name</span><span> )
DOMProcessingInstruction createProcessingInstruction ( </span><span>string</span> <span>$target</span> [, <span>string</span> <span>$data</span><span> ] )
DOMText createTextNode ( </span><span>string</span> <span>$content</span><span> )
DOMElement getElementById ( </span><span>string</span> <span>$elementId</span><span> )
DOMNodeList getElementsByTagName ( </span><span>string</span> <span>$name</span><span> )
DOMNodeList getElementsByTagNameNS ( </span><span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span><span> )
DOMNode importNode ( DOMNode </span><span>$importedNode</span> [, bool <span>$deep</span><span> ] )
</span><span>mixed</span> load ( <span>string</span> <span>$filename</span> [, int <span>$options</span>=0<span> ] )
bool loadHTML ( </span><span>string</span> <span>$source</span><span> )
bool loadHTMLFile ( </span><span>string</span> <span>$filename</span><span> )
</span><span>mixed</span> loadXML ( <span>string</span> <span>$source</span> [, int <span>$options</span>=0<span> ] )
void normalizeDocument ( void )
bool registerNodeClass ( </span><span>string</span> <span>$baseclass</span> , <span>string</span> <span>$extendedclass</span><span> )
bool relaxNGValidate ( </span><span>string</span> <span>$filename</span><span> )
bool relaxNGValidateSource ( </span><span>string</span> <span>$source</span><span> )
int save ( </span><span>string</span> <span>$filename</span> [, int <span>$options</span><span> ] )
</span><span>string</span><span> saveHTML ( void )
int saveHTMLFile ( </span><span>string</span> <span>$filename</span><span> )
</span><span>string</span> saveXML ([ DOMNode <span>$node</span> [, int <span>$options</span><span> ]] )
bool schemaValidate ( </span><span>string</span> <span>$filename</span><span> )
bool schemaValidateSource ( </span><span>string</span> <span>$source</span><span> )
bool validate ( void )
int xinclude ([ int </span><span>$options</span><span> ] )
</span><span>/*</span><span> Inherited methods </span><span>*/</span><span>
DOMNode DOMNode</span>::appendChild ( DOMNode <span>$newnode</span><span> )
DOMNode DOMNode</span>::cloneNode ([ bool <span>$deep</span><span> ] )
bool DOMNode</span>::<span>hasAttributes ( void )
bool DOMNode</span>::<span>hasChildNodes ( void )
DOMNode DOMNode</span>::insertBefore ( DOMNode <span>$newnode</span> [, DOMNode <span>$refnode</span><span> ] )
bool DOMNode</span>::isDefaultNamespace ( <span>string</span> <span>$namespaceURI</span><span> )
bool DOMNode</span>::isSameNode ( DOMNode <span>$node</span><span> )
bool DOMNode</span>::isSupported ( <span>string</span> <span>$feature</span> , <span>string</span> <span>$version</span><span> )
</span><span>string</span> DOMNode::lookupNamespaceURI ( <span>string</span> <span>$prefix</span><span> )
</span><span>string</span> DOMNode::lookupPrefix ( <span>string</span> <span>$namespaceURI</span><span> )
void DOMNode</span>::<span>normalize ( void )
DOMNode DOMNode</span>::removeChild ( DOMNode <span>$oldnode</span><span> )
DOMNode DOMNode</span>::replaceChild ( DOMNode <span>$newnode</span> , DOMNode <span>$oldnode</span><span> )
}</span>

元素节点类DOMElement类型定义如下:

<span> 1</span> DOMElement <span>extends</span><span> DOMNode {
</span><span> 2</span> <span>/*</span><span> Properties </span><span>*/</span>
<span> 3</span> readonly <span>public</span> bool <span>$schemaTypeInfo</span><span> ;
</span><span> 4</span> readonly <span>public</span> <span>string</span> <span>$tagName</span><span> ;
</span><span> 5</span> <span>/*</span><span> Methods </span><span>*/</span>
<span> 6</span> __construct ( <span>string</span> <span>$name</span> [, <span>string</span> <span>$value</span> [, <span>string</span> <span>$namespaceURI</span><span> ]] )
</span><span> 7</span> <span>string</span> getAttribute ( <span>string</span> <span>$name</span><span> )
</span><span> 8</span> DOMAttr getAttributeNode ( <span>string</span> <span>$name</span><span> )
</span><span> 9</span> DOMAttr getAttributeNodeNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span><span> )
</span><span>10</span> <span>string</span> getAttributeNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span><span> )
</span><span>11</span> DOMNodeList getElementsByTagName ( <span>string</span> <span>$name</span><span> )
</span><span>12</span> DOMNodeList getElementsByTagNameNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span><span> )
</span><span>13</span> bool hasAttribute ( <span>string</span> <span>$name</span><span> )
</span><span>14</span> bool hasAttributeNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span><span> )
</span><span>15</span> bool removeAttribute ( <span>string</span> <span>$name</span><span> )
</span><span>16</span> bool removeAttributeNode ( DOMAttr <span>$oldnode</span><span> )
</span><span>17</span> bool removeAttributeNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span><span> )
</span><span>18</span> DOMAttr setAttribute ( <span>string</span> <span>$name</span> , <span>string</span> <span>$value</span><span> )
</span><span>19</span> DOMAttr setAttributeNode ( DOMAttr <span>$attr</span><span> )
</span><span>20</span> DOMAttr setAttributeNodeNS ( DOMAttr <span>$attr</span><span> )
</span><span>21</span> void setAttributeNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$qualifiedName</span> , <span>string</span> <span>$value</span><span> )
</span><span>22</span> void setIdAttribute ( <span>string</span> <span>$name</span> , bool <span>$isId</span><span> )
</span><span>23</span> void setIdAttributeNode ( DOMAttr <span>$attr</span> , bool <span>$isId</span><span> )
</span><span>24</span> void setIdAttributeNS ( <span>string</span> <span>$namespaceURI</span> , <span>string</span> <span>$localName</span> , bool <span>$isId</span><span> )
</span><span>25</span> <span>/*</span><span> Inherited methods </span><span>*/</span>
<span>26</span> DOMNode DOMNode::appendChild ( DOMNode <span>$newnode</span><span> )
</span><span>27</span> DOMNode DOMNode::cloneNode ([ bool <span>$deep</span><span> ] )
</span><span>28</span> bool DOMNode::<span>hasAttributes ( void )
</span><span>29</span> bool DOMNode::<span>hasChildNodes ( void )
</span><span>30</span> DOMNode DOMNode::insertBefore ( DOMNode <span>$newnode</span> [, DOMNode <span>$refnode</span><span> ] )
</span><span>31</span> bool DOMNode::isDefaultNamespace ( <span>string</span> <span>$namespaceURI</span><span> )
</span><span>32</span> bool DOMNode::isSameNode ( DOMNode <span>$node</span><span> )
</span><span>33</span> bool DOMNode::isSupported ( <span>string</span> <span>$feature</span> , <span>string</span> <span>$version</span><span> )
</span><span>34</span> <span>string</span> DOMNode::lookupNamespaceURI ( <span>string</span> <span>$prefix</span><span> )
</span><span>35</span> <span>string</span> DOMNode::lookupPrefix ( <span>string</span> <span>$namespaceURI</span><span> )
</span><span>36</span> void DOMNode::<span>normalize ( void )
</span><span>37</span> DOMNode DOMNode::removeChild ( DOMNode <span>$oldnode</span><span> )
</span><span>38</span> DOMNode DOMNode::replaceChild ( DOMNode <span>$newnode</span> , DOMNode <span>$oldnode</span><span> )
</span><span>39</span> }

文本类型DOMText定义如下:

DOMText <span>extends</span><span> DOMCharacterData {
</span><span>/*</span><span> Properties </span><span>*/</span><span>
readonly </span><span>public</span> <span>string</span> <span>$wholeText</span><span> ;
</span><span>/*</span><span> Methods </span><span>*/</span><span>
__construct ([ </span><span>string</span> <span>$value</span><span> ] )
bool isWhitespaceInElementContent ( void )
DOMText splitText ( int </span><span>$offset</span><span> )
</span><span>/*</span><span> Inherited methods </span><span>*/</span><span>
void DOMCharacterData</span>::appendData ( <span>string</span> <span>$data</span><span> )
void DOMCharacterData</span>::deleteData ( int <span>$offset</span> , int <span>$count</span><span> )
void DOMCharacterData</span>::insertData ( int <span>$offset</span> , <span>string</span> <span>$data</span><span> )
void DOMCharacterData</span>::replaceData ( int <span>$offset</span> , int <span>$count</span> , <span>string</span> <span>$data</span><span> )
</span><span>string</span> DOMCharacterData::substringData ( int <span>$offset</span> , int <span>$count</span><span> )
}</span>

DOMNode节点定义如下:

<span>DOMNode {
</span><span>/*</span><span> Properties </span><span>*/</span>
<span>public</span> readonly <span>string</span> <span>$nodeName</span><span> ;
</span><span>public</span> <span>string</span> <span>$nodeValue</span><span> ;
</span><span>public</span> readonly int <span>$nodeType</span><span> ;
</span><span>public</span> readonly DOMNode <span>$parentNode</span><span> ;
</span><span>public</span> readonly DOMNodeList <span>$childNodes</span><span> ;
</span><span>public</span> readonly DOMNode <span>$firstChild</span><span> ;
</span><span>public</span> readonly DOMNode <span>$lastChild</span><span> ;
</span><span>public</span> readonly DOMNode <span>$previousSibling</span><span> ;
</span><span>public</span> readonly DOMNode <span>$nextSibling</span><span> ;
</span><span>public</span> readonly DOMNamedNodeMap <span>$attributes</span><span> ;
</span><span>public</span> readonly DOMDocument <span>$ownerDocument</span><span> ;
</span><span>public</span> readonly <span>string</span> <span>$namespaceURI</span><span> ;
</span><span>public</span> <span>string</span> <span>$prefix</span><span> ;
</span><span>public</span> readonly <span>string</span> <span>$localName</span><span> ;
</span><span>public</span> readonly <span>string</span> <span>$baseURI</span><span> ;
</span><span>public</span> <span>string</span> <span>$textContent</span><span> ;
</span><span>/*</span><span> Methods </span><span>*/</span><span>
DOMNode appendChild ( DOMNode </span><span>$newnode</span><span> )
DOMNode cloneNode ([ bool </span><span>$deep</span><span> ] )
bool hasAttributes ( void )
bool hasChildNodes ( void )
DOMNode insertBefore ( DOMNode </span><span>$newnode</span> [, DOMNode <span>$refnode</span><span> ] )
bool isDefaultNamespace ( </span><span>string</span> <span>$namespaceURI</span><span> )
bool isSameNode ( DOMNode </span><span>$node</span><span> )
bool isSupported ( </span><span>string</span> <span>$feature</span> , <span>string</span> <span>$version</span><span> )
</span><span>string</span> lookupNamespaceURI ( <span>string</span> <span>$prefix</span><span> )
</span><span>string</span> lookupPrefix ( <span>string</span> <span>$namespaceURI</span><span> )
void normalize ( void )
DOMNode removeChild ( DOMNode </span><span>$oldnode</span><span> )
DOMNode replaceChild ( DOMNode </span><span>$newnode</span> , DOMNode <span>$oldnode</span><span> )
}</span>

3、测试程序

<span>  1</span> <span>php
</span><span>  2</span> 
<span>  3</span> <span>const</span> INDEX_FILE_NAME = "student_file_index.xml"<span>;
</span><span>  4</span> 
<span>  5</span> <span>//</span><span>文件索引类</span>
<span>  6</span> <span>class</span><span> file_index
</span><span>  7</span> <span>{
</span><span>  8</span>   <span>public</span> <span>function</span> set_file_index(<span>$file_name</span>, <span>$cur_count</span>, <span>$total_count</span><span>)
</span><span>  9</span> <span>  {
</span><span> 10</span>     <span>$this</span>->file_name = <span>$file_name</span><span>;
</span><span> 11</span>     <span>$this</span>->cur_count = <span>$cur_count</span><span>;
</span><span> 12</span>     <span>$this</span>->total_count = <span>$total_count</span><span>;
</span><span> 13</span> <span>  }
</span><span> 14</span>   <span>public</span> <span>function</span><span> get_file_name()
</span><span> 15</span> <span>  {
</span><span> 16</span>     <span>return</span> <span>$this</span>-><span>file_name;
</span><span> 17</span> <span>  }
</span><span> 18</span>   <span>public</span> <span>function</span><span> get_cur_count()
</span><span> 19</span> <span>  {
</span><span> 20</span>     <span>return</span> <span>$this</span>-><span>cur_count;
</span><span> 21</span> <span>  }
</span><span> 22</span>   <span>public</span> <span>function</span><span> get_total_count()
</span><span> 23</span> <span>  {
</span><span> 24</span>     <span>return</span> <span>$this</span>-><span>total_count;
</span><span> 25</span> <span>  }
</span><span> 26</span> 
<span> 27</span>   <span>private</span> <span>$file_name</span>;  <span>//</span><span>文件名</span>
<span> 28</span>   <span>private</span> <span>$cur_count</span>;  <span>//</span><span>当前记录数</span>
<span> 29</span>   <span>private</span> <span>$total_count</span>; <span>//</span><span>总记录数</span>
<span> 30</span> <span>}
</span><span> 31</span> 
<span> 32</span> <span>function</span> create_file_index(<span>array</span> <span>$params</span><span>)
</span><span> 33</span> <span>{
</span><span> 34</span>   <span>$index</span> = <span>new</span><span> file_index();
</span><span> 35</span>   <span>$index</span>->set_file_index(<span>$params</span>['file_name'], 
<span> 36</span>       <span>$params</span>['cur_count'], <span>$params</span>['total_count'<span>]);
</span><span> 37</span>   <span>return</span> <span>$index</span><span>;
</span><span> 38</span> <span>}
</span><span> 39</span> 
<span> 40</span> <span>function</span> create_file_node(DOMDocument <span>$doc</span>, file_index <span>$index</span><span>)
</span><span> 41</span> <span>{
</span><span> 42</span>   <span>//</span><span>创建一个file元素</span>
<span> 43</span>   <span>$file</span> = <span>$doc</span>->createElement("file"<span>);
</span><span> 44</span>   <span>//</span><span>创建一个属性元素</span>
<span> 45</span>   <span>$name_attr</span> = <span>$doc</span>->createAttribute("name"<span>);
</span><span> 46</span>   <span>//</span><span>将该属性添加到file元素上</span>
<span> 47</span>   <span>$file</span>->appendChild(<span>$name_attr</span><span>);
</span><span> 48</span> 
<span> 49</span>   <span>//</span><span>创建一个文本元素</span>
<span> 50</span>   <span>$file_name</span> = <span>$doc</span>->createTextNode(<span>$index</span>-><span>get_file_name());
</span><span> 51</span>   <span>//</span><span>将文本元素添加name_attr属性上</span>
<span> 52</span>   <span>$name_attr</span>->appendChild(<span>$file_name</span><span>);
</span><span> 53</span> 
<span> 54</span>   <span>//</span><span>创建一个cur_count元素</span>
<span> 55</span>   <span>$cur_count</span> = <span>$doc</span>->createElement("cur_count", <span>strval</span>(<span>$index</span>-><span>get_cur_count()));
</span><span> 56</span>   <span>//</span><span>将cur_count添加到file元素下</span>
<span> 57</span>   <span>$cur_count</span> = <span>$file</span>->appendChild(<span>$cur_count</span><span>);
</span><span> 58</span> 
<span> 59</span>   <span>//</span><span>创建一个total_count元素</span>
<span> 60</span>   <span>$total_count</span> = <span>$doc</span>->createElement("total_count",
<span> 61</span>       <span>strval</span>(<span>$index</span>-><span>get_total_count()));
</span><span> 62</span>   <span>//</span><span>将total_count添加到file元素下</span>
<span> 63</span>   <span>$total_count</span> = <span>$file</span>->appendChild(<span>$total_count</span><span>);
</span><span> 64</span> 
<span> 65</span>   <span>return</span> <span>$file</span><span>;
</span><span> 66</span> <span>}
</span><span> 67</span> 
<span> 68</span> <span>function</span> create_index_file(<span>$index_file_name</span>, <span>array</span> <span>$params</span><span>)
</span><span> 69</span> <span>{
</span><span> 70</span>   <span>//</span><span>创建一个文档</span>
<span> 71</span>   <span>$doc</span> = <span>new</span> DOMDocument("1.0", "utf-8"<span>);
</span><span> 72</span>   <span>//</span><span>创建根元素</span>
<span> 73</span>   <span>$root</span> = <span>$doc</span>->createElement("index"<span>);
</span><span> 74</span>   <span>$root</span> = <span>$doc</span>->appendChild(<span>$root</span><span>);
</span><span> 75</span> 
<span> 76</span>   <span>//</span><span>创建一个索引结构</span>
<span> 77</span>   <span>$index</span> = create_file_index(<span>$params</span><span>);
</span><span> 78</span>   <span>$file</span> = create_file_node(<span>$doc</span>, <span>$index</span><span>);
</span><span> 79</span> 
<span> 80</span>   <span>//</span><span>将file看到根元素下 </span>
<span> 81</span>   <span>$root</span>->appendChild(<span>$file</span><span>);
</span><span> 82</span>   <span>$doc</span>->save(<span>$index_file_name</span><span>);
</span><span> 83</span>   <span>return</span> <span>true</span><span>;    
</span><span> 84</span> <span>}
</span><span> 85</span> 
<span> 86</span> <span>function</span> add_index_file(<span>$index_file_name</span>, <span>array</span> <span>$params</span><span>)
</span><span> 87</span> <span>{
</span><span> 88</span>   <span>//</span><span>创建一个文档</span>
<span> 89</span>   <span>$doc</span> = <span>new</span><span> DOMDocument();
</span><span> 90</span>   <span>//</span><span>加载xml文件</span>
<span> 91</span>   <span>$doc</span>->load(<span>$index_file_name</span><span>);
</span><span> 92</span>   <span>//</span><span>获取index元素列表</span>
<span> 93</span>   <span>$node_list</span> = <span>$doc</span>->getElementsByTagName('index'<span>);
</span><span> 94</span>   <span>//</span><span>获取根元素</span>
<span> 95</span>   <span>$root</span> = <span>$node_list</span>->item(0<span>);
</span><span> 96</span>   <span>//</span><span>创建一个索引结构</span>
<span> 97</span>   <span>$index</span> = create_file_index(<span>$params</span><span>);
</span><span> 98</span>   <span>$file</span> = create_file_node(<span>$doc</span>, <span>$index</span><span>);
</span><span> 99</span>   <span>//</span><span>将file看到根元素下 </span>
<span>100</span>   <span>$root</span>->appendChild(<span>$file</span><span>);
</span><span>101</span>   <span>$doc</span>->save(<span>$index_file_name</span><span>);
</span><span>102</span> <span>}
</span><span>103</span> 
<span>104</span> <span>function</span> traverse_file_index(<span>$index_file_name</span><span>)
</span><span>105</span> <span>{
</span><span>106</span>   <span>$file_index</span> = <span>array</span><span>();
</span><span>107</span>   <span>$doc</span> = <span>new</span><span> DOMDocument();
</span><span>108</span>   <span>$doc</span>->load(<span>$index_file_name</span><span>);
</span><span>109</span>   <span>//</span><span>获取file元素集合</span>
<span>110</span>   <span>$file_list</span> = <span>$doc</span>->getElementsByTagName('file'<span>);
</span><span>111</span>   <span>//</span><span>获取cur_count元素集合</span>
<span>112</span>   <span>$cur_count_list</span> = <span>$doc</span>->getElementsByTagName('cur_count'<span>);
</span><span>113</span>   <span>//</span><span>获取total_count元素集合</span>
<span>114</span>   <span>$total_count_list</span> = <span>$doc</span>->getElementsByTagName('total_count'<span>);
</span><span>115</span>   <span>for</span> (<span>$i</span> = 0; <span>$i</span> $file_list->length; <span>$i</span>++<span>) {
</span><span>116</span>     <span>$index</span> = <span>new</span><span> file_index();
</span><span>117</span>     <span>//</span><span>获取file元素的name属性值</span>
<span>118</span>     <span>$file_name</span> = <span>$file_list</span>->item(<span>$i</span>)->attributes->getNamedItem("name")-><span>nodeValue;
</span><span>119</span>     <span>$index</span>->set_file_index(<span>$file_name</span>, <span>$cur_count_list</span>->item(<span>$i</span>)->nodeValue,
<span>120</span>         <span>$total_count_list</span>->item(<span>$i</span>)-><span>nodeValue);
</span><span>121</span>     <span>$file_index</span>[<span>$i</span>] = <span>$index</span><span>;
</span><span>122</span> <span>  }
</span><span>123</span> 
<span>124</span>   <span>return</span> <span>$file_index</span><span>;
</span><span>125</span> <span>}
</span><span>126</span> 
<span>127</span> <span>/*</span><span>****************for test********************</span><span>*/</span>
<span>128</span> <span>$params</span> = <span>array</span><span>();
</span><span>129</span> <span>$index_file_name</span> =<span> INDEX_FILE_NAME;
</span><span>130</span> 
<span>131</span> <span>if</span> (<span>file_exists</span>(<span>$index_file_name</span><span>)) {
</span><span>132</span>   <span>$params</span>['file_name'] = "student_info_2014_02_12"<span>;
</span><span>133</span>   <span>$params</span>['cur_count'] = 10<span>;
</span><span>134</span>   <span>$params</span>['total_count'] = 10<span>;
</span><span>135</span>   <span>echo</span> "Add index to file.\n"<span>;
</span><span>136</span>   add_index_file(<span>$index_file_name</span>, <span>$params</span><span>);
</span><span>137</span> <span>}
</span><span>138</span> <span>else</span><span> {
</span><span>139</span>   <span>$params</span>['file_name'] = "student_info_2014_02_11"<span>;
</span><span>140</span>   <span>$params</span>['cur_count'] = 23<span>;
</span><span>141</span>   <span>$params</span>['total_count'] = 33<span>;
</span><span>142</span>   <span>echo</span> "Create index file.\n"<span>;
</span><span>143</span>   create_index_file(<span>$index_file_name</span>, <span>$params</span><span>);
</span><span>144</span> <span>}
</span><span>145</span> 
<span>146</span> <span>//</span><span>测试读取xm文件</span>
<span>147</span> <span>echo</span> "Read index content from ".<span>$index_file_name</span>."\n"<span>;
</span><span>148</span> <span>echo</span> "file_name\t\t\tcur_count\ttotal_count.\n"<span>;
</span><span>149</span> <span>$file_index</span> = traverse_file_index(<span>$index_file_name</span><span>);
</span><span>150</span> <span>foreach</span>(<span>$file_index</span> <span>as</span> <span>$index</span><span>) {
</span><span>151</span>   <span>echo</span> <span>$index</span>-><span>get_file_name();
</span><span>152</span>   <span>echo</span> "\t\t  "<span>;
</span><span>153</span>   <span>echo</span> <span>$index</span>-><span>get_cur_count();
</span><span>154</span>   <span>echo</span> <span>strval</span>(<span>$cur_count</span><span>);
</span><span>155</span>   <span>echo</span> "\t\t  "<span>;
</span><span>156</span>   <span>echo</span> <span>$index</span>-><span>get_total_count();
</span><span>157</span>   <span>echo</span> "\n"<span>;
</span><span>158</span> }

测试结果如下图所示:

PHP使用DOM XML操作XML[总结]

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn