对于XML的操作包括遍历,生成,修改,删除等其他类似的操作。PHP对于XML的操作方式很多,这次学习的是通过DOMDocument进行操作,其他的操作方法可以参考 http://www.oschina.net/code/snippet_110138_4727 1.对XML文件的遍历 通过DOMDocument对于XML文件的操
对于XML的操作包括遍历,生成,修改,删除等其他类似的操作。PHP对于XML的操作方式很多,这次学习的是通过DOMDocument进行操作,其他的操作方法可以参考
http://www.oschina.net/code/snippet_110138_4727
1.对XML文件的遍历
通过DOMDocument对于XML文件的操作的方法:首先要实例化一个DOMDocument类的对象,然后引入要操作的XML文件。即:
<span>1</span> <span>$doc</span> = <span>new</span><span> DOMDocument(); </span><span>2</span> <span>$doc</span>->load("test.xml");
<span></span><span>xml version="1.0" encoding="utf-8" </span><span>?></span> <span><span>school</span><span>></span> <span><span>student</span><span>></span> <span><span>name</span><span>></span>张三<span></span><span>name</span><span>></span> <span><span>sex</span><span>></span>男<span></span><span>sex</span><span>></span> <span><span>age</span><span>></span>19<span></span><span>age</span><span>></span> <span></span><span>student</span><span>></span> <span><span>student</span><span>></span> <span><span>name</span><span>></span>李四<span></span><span>name</span><span>></span> <span><span>sex</span><span>></span>女<span></span><span>sex</span><span>></span> <span><span>age</span><span>></span>18<span></span><span>age</span><span>></span> <span></span><span>student</span><span>></span> <span><span>student</span><span>></span> <span><span>name</span><span>></span>王五<span></span><span>name</span><span>></span> <span><span>sex</span><span>></span>男<span></span><span>sex</span><span>></span> <span><span>age</span><span>></span>19<span></span><span>age</span><span>></span> <span></span><span>student</span><span>></span> <span></span><span>school</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span>1</span> <span>$content</span> = <span>$doc</span>->getElementsByTagName("student"<span>); </span><span>2</span> <span>foreach</span>(<span>$content</span> <span>as</span> <span>$value</span><span>){ </span><span>3</span> <span>echo</span> <span>$value</span>->nodeValue."<br>"<span>; </span><span>4</span> }
注意:在遍历XML文件的时候,要注意的一个问题就是编码的问题,如果xml文件的encoding为"utf-8",那么xml文件的文件编码也必须是utf-8,可以通过文本编辑器将文件另存为和xml的encoding相同的文件编码即可。否则在遍历的时候会报错或者出现乱码。
2.生成XML文件
在生成XML文件的时候,是通过DOMDocument类中的方法来创建各个节点和节点内容的。我通过查询数据库,并将数据库中的信息生成XML文件方法如下:
<span> 1</span> <span>include</span> "database.php"; <span>//</span><span>导入连接数据库文件</span> <span> 2</span> <span>$query</span> = <span>mysql_query</span>("SELECT * FROM test"<span>); </span><span> 3</span> <span> 4</span> <span>//</span><span>操作xml准备</span> <span> 5</span> <span>$doc</span> = <span>new</span><span> DOMDocument("1.0","utf-8"); </span><span> 6</span> <span> 7</span> <span>//</span><span>通过createElement方法为xml文件创建节点 </span><span> 8</span> <span>//创建根节点</span> <span> 9</span> <span>$school</span>= <span>$doc</span>->createElement("school"<span>); </span><span>10</span> <span>//</span><span>11</span> <span>//将根节点添加到xml文件中</span> <span>12</span> <span>$doc</span>->appendChild(<span>$school</span><span>); </span><span>13</span> <span>14</span> <span>//</span><span>创建student节点</span> <span>15</span> <span>$student</span>= <span>$doc</span>->createElement("student"<span>); </span><span>16</span> <span>//</span><span>将student节点添加到school节点下</span> <span>17</span> <span>$school</span>->appendChild(<span>$student</span><span>); </span><span>18</span> <span>19</span> <span>$nameinfo</span> = <span>array</span>(); <span>//</span> <span>20</span> <span>$sexinfo</span> = <span>array</span>(); <span>//</span> <span>21</span> <span>$ageinfo</span> = <span>array</span>(); <span>//</span> <span>22</span> <span>$idinfo</span> = <span>array</span>(); <span>//</span><span>对应xml文件中属性id的值</span> <span>23</span> <span>$i</span> = 0; <span>//</span><span>控制变量i</span> <span>24</span> <span>25</span> <span>while</span>(<span>$row</span>=<span>mysql_fetch_array</span>(<span>$query</span><span>)){ </span><span>26</span> <span>//</span><span>27</span> <span> //查询数据库信息并赋值给相应的数组</span> <span>28</span> <span>$nameinfo</span>[<span>$i</span>] = <span>$doc</span>->createTextNode(<span>$row</span>["name"<span>]); </span><span>29</span> <span>$sexinfo</span>[<span>$i</span>] = <span>$doc</span>->createTextNode(<span>$row</span>["sex"<span>]); </span><span>30</span> <span>$ageinfo</span>[<span>$i</span>] = <span>$doc</span>->createTextNode(<span>$row</span>["age"<span>]); </span><span>31</span> <span>$idinfo</span>[<span>$i</span>] = <span>$doc</span>->createTextNode(<span>$i</span><span>); </span><span>32</span> <span>$i</span>++<span>; </span><span>33</span> <span>} </span><span>34</span> <span>35</span> <span>for</span>(<span>$i</span>=0;<span>$i</span>count(<span>$nameinfo</span>);<span>$i</span>++<span>){ </span><span>36</span> <span>$name</span> = <span>$doc</span>->createElement("name"<span>); </span><span>37</span> <span>$sex</span> = <span>$doc</span>->createElement("sex"<span>); </span><span>38</span> <span>$age</span> = <span>$doc</span>->createElement("age"<span>); </span><span>39</span> <span>//</span> <span>40</span> <span>$id</span> = <span>$doc</span>->createAttribute("id"<span>); </span><span>41</span> <span>42</span> <span>//</span> <span>43</span> <span>$student</span>->appendChild(<span>$name</span><span>); </span><span>44</span> <span>$student</span>->appendChild(<span>$sex</span><span>); </span><span>45</span> <span>$student</span>->appendChild(<span>$age</span><span>); </span><span>46</span> <span>$student</span>->appendChild(<span>$id</span><span>); </span><span>47</span> <span>48</span> <span>//</span> <span>49</span> <span>$name</span>->appendChild(<span>$nameinfo</span>[<span>$i</span><span>]); </span><span>50</span> <span>$sex</span>->appendChild(<span>$sexinfo</span>[<span>$i</span><span>]); </span><span>51</span> <span>$age</span>->appendChild(<span>$ageinfo</span>[<span>$i</span><span>]); </span><span>52</span> <span>$id</span>->appendChild(<span>$idinfo</span>[<span>$i</span><span>]); </span><span>53</span> <span>} </span><span>54</span> <span>55</span> <span>//</span><span>通过save方法将生成的xml文件保存</span> <span>56</span> <span>$doc</span>->save("test.xml");
注意:在生成XML文件的时候同样应该注意编码问题,除了XML文件编码和XML指定编码应当一致以外,在连接数据库的时候还应该注意XML指定的编码应该和数据库的编
3.XML文件的修改和删除
<span> 1</span> <span>$doc</span>= <span>new</span><span> DOMDocument(); </span><span> 2</span> <span>$doc</span>->load("test.xml"<span>); </span><span> 3</span> <span> 4</span> <span>//</span> <span> 5</span> <span>$students</span> = <span>$doc</span>->getElementsByTagName("student"<span>); </span><span> 6</span> <span> 7</span> <span>//</span><span>遍历$students,将每个student的信息存为$student</span> <span> 8</span> <span>foreach</span>(<span>$students</span> <span>as</span> <span>$student</span><span>){ </span><span> 9</span> <span>//</span><span>通过getAttribute获取指定属性的值 </span><span>10</span> <span>11</span> <span>if</span>(<span>$student</span>->getAttribute("id")==2<span>){ </span><span>12</span> <span>$content</span> = <span>$doc</span>->getElementsByTagName("name")->item(0)->nodeValue = "Kenticny"<span>; </span><span>13</span> <span> } </span><span>14</span> <span>//</span> <span>15</span> <span>if</span>(<span>$student</span>->getAttribute("id")==1<span>){ </span><span>16</span> <span>//</span><span>17</span> <span>18</span> <span>$student</span>->parentNode->removeChild(<span>$student</span><span>); </span><span>19</span> <span> } </span><span>20</span> <span>21</span> <span>} </span><span>22</span> <span>//</span><span>保存修改的XML文件 </span> <span>23</span> <span>$doc</span>->save("test.xml");
操作。