PHPl使用DOM XML操作XML
添加時間:2014-12-9 1:45:15
添加:
思海網絡
1、前言
XML樹狀層次結構鮮明,非常適合作為配置文件。PHP中可以使用DOM XML來操作XML。本文總結一下PHP使用DOM XML創建、添加節點、查詢XML文件。
2、使用DOM XML
XML中節點分為元素和文本,DOMDocument類型是文檔類型,提供了操作元素和文本的成員函數和屬性。
DOMDocument類如下:
復制代碼
DOMDocument extends DOMNode {
/* Properties */
readonly public string $actualEncoding ;
readonly public DOMConfiguration $config ;
readonly public DOMDocumentType $doctype ;
readonly public DOMElement $documentElement ;
public string $documentURI ;
public string $encoding ;
public bool $formatOutput ;
readonly public DOMImplementation $implementation ;
public bool $preserveWhiteSpace =true ;
public bool $recover ;
public bool $resolveExternals ;
public bool $standalone ;
public bool $strictErrorChecking =true ;
public bool $substituteEntities ;
public bool $validateOnParse =false ;
public string $version ;
readonly public string $xmlEncoding ;
public bool $xmlStandalone ;
public string $xmlVersion ;
/* Methods */
__construct ([ string $version [, string $encoding ]] )
DOMAttr createAttribute ( string $name )
DOMAttr createAttributeNS ( string $namespaceURI , string $qualifiedName )
DOMCDATASection createCDATASection ( string $data )
DOMComment createComment ( string $data )
DOMDocumentFragment createDocumentFragment ( void )
DOMElement createElement ( string $name [, string $value ] )
DOMElement createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )
DOMEntityReference createEntityReference ( string $name )
DOMProcessingInstruction createProcessingInstruction ( string $target [, string $data ] )
DOMText createTextNode ( string $content )
DOMElement getElementById ( string $elementId )
DOMNodeList getElementsByTagName ( string $name )
DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )
DOMNode importNode ( DOMNode $importedNode [, bool $deep ] )
mixed load ( string $filename [, int $options=0 ] )
bool loadHTML ( string $source )
bool loadHTMLFile ( string $filename )
mixed loadXML ( string $source [, int $options=0 ] )
void normalizeDocument ( void )
bool registerNodeClass ( string $baseclass , string $extendedclass )
bool relaxNGValidate ( string $filename )
bool relaxNGValidateSource ( string $source )
int save ( string $filename [, int $options ] )
string saveHTML ( void )
int saveHTMLFile ( string $filename )
string saveXML ([ DOMNode $node [, int $options ]] )
bool schemaValidate ( string $filename )
bool schemaValidateSource ( string $source )
bool validate ( void )
int xinclude ([ int $options ] )
/* Inherited methods */
DOMNode DOMNode::appendChild ( DOMNode $newnode )
DOMNode DOMNode::cloneNode ([ bool $deep ] )
bool DOMNode::hasAttributes ( void )
bool DOMNode::hasChildNodes ( void )
DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
bool DOMNode::isDefaultNamespace ( string $namespaceURI )
bool DOMNode::isSameNode ( DOMNode $node )
bool DOMNode::isSupported ( string $feature , string $version )
string DOMNode::lookupNamespaceURI ( string $prefix )
string DOMNode::lookupPrefix ( string $namespaceURI )
void DOMNode::normalize ( void )
DOMNode DOMNode::removeChild ( DOMNode $oldnode )
DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}
復制代碼
元素節點類DOMElement類型定義如下:
復制代碼
1 DOMElement extends DOMNode {
2 /* Properties */
3 readonly public bool $schemaTypeInfo ;
4 readonly public string $tagName ;
5 /* Methods */
6 __construct ( string $name [, string $value [, string $namespaceURI ]] )
7 string getAttribute ( string $name )
8 DOMAttr getAttributeNode ( string $name )
9 DOMAttr getAttributeNodeNS ( string $namespaceURI , string $localName )
10 string getAttributeNS ( string $namespaceURI , string $localName )
11 DOMNodeList getElementsByTagName ( string $name )
12 DOMNodeList getElementsByTagNameNS ( string $namespaceURI , string $localName )
13 bool hasAttribute ( string $name )
14 bool hasAttributeNS ( string $namespaceURI , string $localName )
15 bool removeAttribute ( string $name )
16 bool removeAttributeNode ( DOMAttr $oldnode )
17 bool removeAttributeNS ( string $namespaceURI , string $localName )
18 DOMAttr setAttribute ( string $name , string $value )
19 DOMAttr setAttributeNode ( DOMAttr $attr )
20 DOMAttr setAttributeNodeNS ( DOMAttr $attr )
21 void setAttributeNS ( string $namespaceURI , string $qualifiedName , string $value )
22 void setIdAttribute ( string $name , bool $isId )
23 void setIdAttributeNode ( DOMAttr $attr , bool $isId )
24 void setIdAttributeNS ( string $namespaceURI , string $localName , bool $isId )
25 /* Inherited methods */
26 DOMNode DOMNode::appendChild ( DOMNode $newnode )
27 DOMNode DOMNode::cloneNode ([ bool $deep ] )
28 bool DOMNode::hasAttributes ( void )
29 bool DOMNode::hasChildNodes ( void )
30 DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
31 bool DOMNode::isDefaultNamespace ( string $namespaceURI )
32 bool DOMNode::isSameNode ( DOMNode $node )
33 bool DOMNode::isSupported ( string $feature , string $version )
34 string DOMNode::lookupNamespaceURI ( string $prefix )
35 string DOMNode::lookupPrefix ( string $namespaceURI )
36 void DOMNode::normalize ( void )
37 DOMNode DOMNode::removeChild ( DOMNode $oldnode )
38 DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
39 }
復制代碼
文本類型DOMText定義如下:
復制代碼
DOMText extends DOMCharacterData {
/* Properties */
readonly public string $wholeText ;
/* Methods */
__construct ([ string $value ] )
bool isWhitespaceInElementContent ( void )
DOMText splitText ( int $offset )
/* Inherited methods */
void DOMCharacterData::appendData ( string $data )
void DOMCharacterData::deleteData ( int $offset , int $count )
void DOMCharacterData::insertData ( int $offset , string $data )
void DOMCharacterData::replaceData ( int $offset , int $count , string $data )
string DOMCharacterData::substringData ( int $offset , int $count )
}
復制代碼
DOMNode節點定義如下:
復制代碼
DOMNode {
/* Properties */
public readonly string $nodeName ;
public string $nodeValue ;
public readonly int $nodeType ;
public readonly DOMNode $parentNode ;
public readonly DOMNodeList $childNodes ;
public readonly DOMNode $firstChild ;
public readonly DOMNode $lastChild ;
public readonly DOMNode $previousSibling ;
public readonly DOMNode $nextSibling ;
public readonly DOMNamedNodeMap $attributes ;
public readonly DOMDocument $ownerDocument ;
public readonly string $namespaceURI ;
public string $prefix ;
public readonly string $localName ;
public readonly string $baseURI ;
public string $textContent ;
/* Methods */
DOMNode appendChild ( DOMNode $newnode )
DOMNode cloneNode ([ bool $deep ] )
bool hasAttributes ( void )
bool hasChildNodes ( void )
DOMNode insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
bool isDefaultNamespace ( string $namespaceURI )
bool isSameNode ( DOMNode $node )
bool isSupported ( string $feature , string $version )
string lookupNamespaceURI ( string $prefix )
string lookupPrefix ( string $namespaceURI )
void normalize ( void )
DOMNode removeChild ( DOMNode $oldnode )
DOMNode replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}
復制代碼
3、測試程序
復制代碼
1 <?php
2
3 const INDEX_FILE_NAME = "student_file_index.xml";
4
5 //文件索引類
6 class file_index
7 {
8 public function set_file_index($file_name, $cur_count, $total_count)
9 {
10 $this->file_name = $file_name;
11 $this->cur_count = $cur_count;
12 $this->total_count = $total_count;
13 }
14 public function get_file_name()
15 {
16 return $this->file_name;
17 }
18 public function get_cur_count()
19 {
20 return $this->cur_count;
21 }
22 public function get_total_count()
23 {
24 return $this->total_count;
25 }
26
27 private $file_name; //文件名
28 private $cur_count; //當前記錄數
29 private $total_count; //總記錄數
30 }
31
32 function create_file_index(array $params)
33 {
34 $index = new file_index();
35 $index->set_file_index($params['file_name'],
36 $params['cur_count'], $params['total_count']);
37 return $index;
38 }
39
40 function create_file_node(DOMDocument $doc, file_index $index)
41 {
42 //創建一個file元素
43 $file = $doc->createElement("file");
44 //創建一個屬性元素
45 $name_attr = $doc->createAttribute("name");
46 //將該屬性添加到file元素上
47 $file->appendChild($name_attr);
48
49 //創建一個文本元素
50 $file_name = $doc->createTextNode($index->get_file_name());
51 //將文本元素添加name_attr屬性上
52 $name_attr->appendChild($file_name);
53
54 //創建一個cur_count元素
55 $cur_count = $doc->createElement("cur_count", strval($index->get_cur_count()));
56 //將cur_count添加到file元素下
57 $cur_count = $file->appendChild($cur_count);
58
59 //創建一個total_count元素
60 $total_count = $doc->createElement("total_count",
61 strval($index->get_total_count()));
62 //將total_count添加到file元素下
63 $total_count = $file->appendChild($total_count);
64
65 return $file;
66 }
67
68 function create_index_file($index_file_name, array $params)
69 {
70 //創建一個文檔
71 $doc = new DOMDocument("1.0", "utf-8");
72 //創建根元素
73 $root = $doc->createElement("index");
74 $root = $doc->appendChild($root);
75
76 //創建一個索引結構
77 $index = create_file_index($params);
78 $file = create_file_node($doc, $index);
79
80 //將file看到根元素下
81 $root->appendChild($file);
82 $doc->save($index_file_name);
83 return true;
84 }
85
86 function add_index_file($index_file_name, array $params)
87 {
88 //創建一個文檔
89 $doc = new DOMDocument();
90 //加載xml文件
91 $doc->load($index_file_name);
92 //獲取index元素列表
93 $node_list = $doc->getElementsByTagName('index');
94 //獲取根元素
95 $root = $node_list->item(0);
96 //創建一個索引結構
97 $index = create_file_index($params);
98 $file = create_file_node($doc, $index);
99 //將file看到根元素下
100 $root->appendChild($file);
101 $doc->save($index_file_name);
102 }
103
104 function traverse_file_index($index_file_name)
105 {
106 $file_index = array();
107 $doc = new DOMDocument();
108 $doc->load($index_file_name);
109 //獲取file元素集合
110 $file_list = $doc->getElementsByTagName('file');
111 //獲取cur_count元素集合
112 $cur_count_list = $doc->getElementsByTagName('cur_count');
113 //獲取total_count元素集合
114 $total_count_list = $doc->getElementsByTagName('total_count');
115 for ($i = 0; $i < $file_list->length; $i++) {
116 $index = new file_index();
117 //獲取file元素的name屬性值
118 $file_name = $file_list->item($i)->attributes->getNamedItem("name")->nodeValue;
119 $index->set_file_index($file_name, $cur_count_list->item($i)->nodeValue,
120 $total_count_list->item($i)->nodeValue);
121 $file_index[$i] = $index;
122 }
123
124 return $file_index;
125 }
126
127 /*****************for test*********************/
128 $params = array();
129 $index_file_name = INDEX_FILE_NAME;
130
131 if (file_exists($index_file_name)) {
132 $params['file_name'] = "student_info_2014_02_12";
133 $params['cur_count'] = 10;
134 $params['total_count'] = 10;
135 echo "Add index to file.\n";
136 add_index_file($index_file_name, $params);
137 }
138 else {
139 $params['file_name'] = "student_info_2014_02_11";
140 $params['cur_count'] = 23;
141 $params['total_count'] = 33;
142 echo "Create index file.\n";
143 create_index_file($index_file_name, $params);
144 }
145
146 //測試讀取xm文件
147 echo "Read index content from ".$index_file_name."\n";
148 echo "file_name\t\t\tcur_count\ttotal_count.\n";
149 $file_index = traverse_file_index($index_file_name);
150 foreach($file_index as $index) {
151 echo $index->get_file_name();
152 echo "\t\t ";
153 echo $index->get_cur_count();
154 echo strval($cur_count);
155 echo "\t\t ";
156 echo $index->get_total_count();
157 echo "\n";
158 }
關鍵字:PHPl、DOM、XML、元素
新文章:
- CentOS7下圖形配置網絡的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統后丟失windows啟動項
- CentOS單網卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網打印機IP講解
- CentOS7使用hostapd實現無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網絡重啟出錯
- 解決Centos7雙系統后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統有什么不同呢
- Centos 6.6默認iptable規則詳解