PHP Pl operating XML with DOMXML [summary] _ PHP Tutorial

Source: Internet
Author: User
PHPl uses DOMXML to operate XML [summary]. 1. preface the XML tree structure is clear and suitable for configuration files. In PHP, DOMXML can be used to operate XML. This article summarizes how PHP uses DOMXML to create, add nodes, and query XML files. 1. Preface

The XML tree structure is distinctive and is suitable for configuration files. In PHP, dom xml can be used to operate XML. This article summarizes how PHP creates, adds, and queries XML files using dom xml.

2. use DOM XML

In XML, nodes are divided into elements and text. the DOMDocument type is document type. It provides the member functions and attributes of Operation elements and text.

The DOMDocument class is as follows:

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: appendChild (DOMNode $ newnode)

DOMNode: cloneNode ([bool $ deep])

Bool DOMNode: hasAttributes (void)

Bool DOMNode: hasChildNodes (void)

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: removeChild (DOMNode $ oldnode)

DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode)

}

The DOMElement type of the element node class is defined as follows:

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: appendChild (DOMNode $ newnode)

27 DOMNode: cloneNode ([bool $ deep])

28 bool DOMNode: hasAttributes (void)

29 bool DOMNode: hasChildNodes (void)

30 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: removeChild (DOMNode $ oldnode)

38 DOMNode: replaceChild (DOMNode $ newnode, DOMNode $ oldnode)

39}

DOMText is defined as follows:

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)

}

The DOMNode node is defined as follows:

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 $ previussibling;

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. test procedure

1

2

3 const INDEX_FILE_NAME = "student_file_index.xml ";

4

5 // File Index

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; // file name

28 private $ cur_count; // number of current Records

29 private $ total_count; // total number of records

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 // create a file element

43 $ file = $ doc-> createElement ("file ");

44 // Create an attribute element

45 $ name_attr = $ doc-> createAttribute ("name ");

46 // add this attribute to the file element

47 $ file-> appendChild ($ name_attr );

48

49 // create a text element

50 $ file_name = $ doc-> createTextNode ($ index-> get_file_name ());

51 // add the name_attr attribute to the text element

52 $ name_attr-> appendChild ($ file_name );

53

54 // Create a cur_count element

55 $ cur_count = $ doc-> createElement ("cur_count", strval ($ index-> get_cur_count ()));

56 // add cur_count to the file element

57 $ cur_count = $ file-> appendChild ($ cur_count );

58

59 // Create a total_count element

60 $ total_count = $ doc-> createElement ("total_count ",

61 strval ($ index-> get_total_count ()));

62 // add total_count to the file element

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 // create a document

71 $ doc = new DOMDocument ("1.0", "UTF-8 ");

72 // Create a root element

73 $ root = $ doc-> createElement ("index ");

74 $ root = $ doc-> appendChild ($ root );

75

76 // create an index structure

77 $ index = create_file_index ($ params );

78 $ file = create_file_node ($ doc, $ index );

79

80 // view the file under the root element

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 // create a document

89 $ doc = new DOMDocument ();

90 // load the xml file

91 $ doc-> load ($ index_file_name );

92 // Retrieve the index element list

93 $ node_list = $ doc-> getElementsByTagName ('index ');

94 // Obtain the root element

95 $ root = $ node_list-> item (0 );

96 // create an index structure

97 $ index = create_file_index ($ params );

98 $ file = create_file_node ($ doc, $ index );

99 // view the file under the root element

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 // Obtain the file element set

110 $ file_list = $ doc-> getElementsByTagName ('file ');

111 // Obtain the cur_count element set

112 $ cur_count_list = $ doc-> getElementsByTagName ('cur _ count ');

113 // Obtain the total_count element set

114 $ total_count_list = $ doc-> getElementsByTagName ('total _ count ');

115 for ($ I = 0; $ I <$ file_list-> length; $ I ++ ){

116 $ index = new file_index ();

117 // Obtain the name attribute value of the file element

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 // test reading the xm file

147 echo "Read index content from". $ index_file_name. "\ n ";

148 echo "file_name \ 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 ";

153 echo $ index-> get_cur_count ();

154 echo strval ($ cur_count );

155 echo "\ t ";

156 echo $ index-> get_total_count ();

157 echo "\ n ";

158}

The hierarchical structure of the lifecycle XML tree is distinct and is suitable for configuration files. In PHP, dom xml can be used to operate XML. This article summarizes how PHP creates, adds, and queries XML files using dom xml...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.