PHP's DOM writes XML

It would be very troublesome to save the link address in the database. Not sure how long the fields are! Some link addresses can be very long. Taking care of those exceptions would leave the database a waste of space. Just save them in an xml file.

The XML file is very simple, only records the URL URI and its id in the database, as follows:

<?xml version="1.0" encoding="UTF-8" ?>
<urlfile>
  <urls>
    <id>0</id>
    <url>http://www.000.com</url>
  </urls>

  <urls>
    <id>1</id>
    <url>http://www.someurl.com</url>
  </urls>
</urlfile>

In the Zend Controller action, use the following code to operate on this myfile.xml:

$xmlpath = $filePath."myfile.xml";

 if(file_exists($xmlpath))
 {

        $dom = new DomDocument('1.0','utf-8');
        $dom->preserveWhiteSpace = false;
        $dom->formatOutput = true;
        $dom->load($xmlpath);

        $newurls = $dom->createElement('urls');  
        $urls = $dom->getElementsByTagName("urlfile")->item(0);  //找到文件追加的位置  
        $w = $urls->appendChild($newurls);               //进行文件追加

        $nid = $dom->createElement('id',$id);            //id节点
        $w->appendChild($nid);

        $nurl = $dom->createElement('url',$url);         //url节点
        $w->appendChild($nurl); 
                  
        $dom->save($xmlpath);                            //保存文件

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325118708&siteId=291194637