php xml 的基本操作类

 1 class xmlMessage{
 2     protected $doc;
 3     protected $rootKey;
 4     public function  __construct() {
 5         $this->doc = new DOMDocument('1.0', 'utf-8');
 6         $this->doc -> formatOutput = true;
 7         $status = $this->doc -> createElement('status');//create new key
 8         $this->rootKey = $status;
 9         $this->doc->appendChild($status);
10     }
11     public function createSon($sonName, $value){
12     $this->deleteChild($sonName);
13     $sonKey = $this->doc -> createElement($sonName);//新建节点
14     $content = $this->doc -> createTextNode($value);//节点值
15     $sonKey -> appendChild($content);
16     $this->rootKey->appendChild($sonKey);
17     }
18     public function appendNodeValue($tagName, $appendValue){
19     if(!$this->hasNodeName($tagName)){
20         $this->createSon($tagName, '');
21     }
22     $this->rootKey->getElementsByTagName($tagName)->item(0)->nodeValue .= "\n".$appendValue;
23     }
24     public function editNodevalue($tagName, $value){
25     if(!$this->hasNodeName($tagName)){
26         $this->createSon($tagName, '');
27     }
28     $this->rootKey->getElementsByTagName($tagName)->item(0)->nodeValue = $value;
29     }
30     public function deleteChild($tagName){
31     if($this->hasNodeName($tagName))
32         $this->rootKey -> removeChild($this->rootKey->getElementsByTagName($tagName)->item(0));
33     }
34     private function hasNodeName($tagName){
35     $hasNode = false;
36     $tempList = $this->doc->getElementsByTagName($tagName);
37     foreach($tempList as $temp){
38         if($temp->nodeName == $tagName)
39         $hasNode = true;
40     }
41     return $hasNode;
42     }
43     public function setNodesByArray($xmlArray){
44     $now = getdate(time());
45     $dataCreated = $now['year'].'/'.$now['mon'].'/'.$now['mday'].' '.$now['hours'].':'.$now['minutes'].':'.$now['seconds'];
46     $this->createSon('language', strtolower($xmlArray['basicInfo']['language']));
47     $this->createSon('source', $xmlArray['basicInfo']['source']);
48     $this->createSon('resumeUrl', $xmlArray['basicInfo']['resumeUrl']);
49     $this->createSon('email', $xmlArray['basicInfo']['email']);
50     $this->createSon('resumeGuid', $xmlArray['basicInfo']['resumeGuid']);
51     $this->createSon('dateCreated', $dataCreated);
52     $this->createSon('success','TRUE');
53     }
54     public function getXML(){
55     return $this->doc->saveXML();
56     }
57 }

 

posted on 2014-06-24 09:18  文淇的技术日记  阅读(174)  评论(0)    收藏  举报

导航