<? Xml version = '1. 0' encoding = 'utf-8'?>
<Root>
<Key id = "5201314" name = "hope you get it done">
<User id = '000000' name = 'out of school '> <! [CDATA [rely on me !!! @ * & ^ $ (* % &]> </Key>
<User id = "6789" name = "not going to school"> 222 </key>
</Key>
<Key id = "5201314" name = "hope you get it done">
<User id = '000000' name = 'out of school '> <! [CDATA [rely on me !!! | ""! @ ": # $ @ $]> </Key>
<User id = "6789" name = "not going to school"> 222 </key>
</Key>
<Key id = "5201314" name = "hope you get it done">
<User id = '000000' name = 'out of school '> <! [CDATA [rely on me !!! <>]> </Key>
<User id = "6789" name = "not going to school"> 222 </key>
</Key>
</Root>
<? Php
/* Func. xml. php 2008-09-20 kilobug
The header attribute must be declared. The values and attributes of each xml node cannot appear <,>, or ". To appear, use CDATA
Each node attribute must use single quotation marks or double quotation marks"
Ignore comments and return an array.
*/
$ XmlContent = ''; // temporary variable
/* Load the xml file and return the array after parsing */
Function loadXML ($ path, $ assoc = null, $ attrassoc = null ){
Return parseXML (@ file_get_contents ($ path), $ assoc, $ attrassoc );
}
/* Parse XMl content and return an array */
Function parseXML ($ xmltext, $ assoc = null, $ attrassoc = null ){
$ Xmldata = array ();
// Convert the top attributes
$ Noderoot = strpos ($ xmltext, '<? ');
$ Noderootspace = strpos ($ xmltext ,'');
$ Nodefoot = strpos ($ xmltext, '?> ');
$ Attr = trim (substr ($ xmltext, $ noderootspace, $ nodefoot-$ noderoot-5 ));
// Conversion node
$ Xmldata = array_merge (
Array (
Array_merge (
Array (substr ($ xmltext, $ noderoot + 2, $ noderootspace-$ noderoot-2 )),
ParseXMLAttrs ($ attr, $ attrassoc)
)
),
ParseXMLNodes (trim (substr ($ xmltext, $ nodefoot + 2), $ assoc, $ attrassoc)
);
Return $ xmldata;
}
/* Parse the XML node and return an array */
Function parseXMLNodes ($ nodestr, $ assoc = null, $ attrassoc = null ){
Global $ xmlContent;
$ XmlContent = $ nodestr;
If (empty ($ xmlContent) |! Strstr ($ xmlContent, '<')){
Return array ();
}
$ Nodedate = array ();
$ I = 0;
While (! Empty ($ xmlContent )){
$ Noderoot = strpos ($ xmlContent, '<');
$ Nodefoot = strpos ($ xmlContent, '> ');
$ Nodeend = strpos ($ xmlContent ,'/');
$ Cdataroot = strpos ($ xmlContent, '<! [CDATA [');
$ Cdatafoot = strpos ($ xmlContent, ']> ');
If ($ noderoot ===$ cdataroot) {// Determine whether CDATA is used
$ Nodedate = substr ($ xmlContent, $ cdataroot + 9, $ cdatafoot-$ cdataroot-9 );
$ XmlContent = substr ($ xmlContent, strpos ($ xmlContent, '>', $ cdatafoot + 3) + 1 );
Break $ nodedate;
} Else if ($ noderoot = $ nodeend-1) {// whether to end the node, Yes returns the previous content
If ($ I = 0 ){
$ Nodedate = substr ($ xmlContent, 0, $ noderoot );
$ XmlContent = substr ($ xmlContent, $ nodefoot + 1 );
Return $ nodedate;
}
Else {
$ XmlContent = substr ($ xmlContent, $ nodefoot + 1 );
Break;
}
}
$ Nodecompart = strpos ($ xmlContent ,'');
// Determine whether there are any attributes
If ($ nodecompart! = False & $ nodecompart> $ noderoot & $ nodecompart <$ nodefoot) {// attributes are available.
$ Nodedate [$ I] [0] = array_merge (array ($ assoc? Substr ($ xmlContent, $ noderoot + 1, $ nodecompart-$ noderoot-1): ''), parseXMLAttrs (substr ($ xmlContent, $ nodecompart + 1, $ nodefoot-$ nodecompart-($ nodefoot-1 ==$ nodeend? 2: 1), $ attrassoc ));
} Else {// no attribute
$ Nodedate [$ I] [0] = array ($ assoc? Substr ($ xmlContent, $ noderoot + 1, $ nodefoot-$ noderoot-1 ):'');
}
$ I ++;
$ XmlContent = substr ($ xmlContent, $ nodefoot + 1); // intercept content after the current node
If ($ nodefoot-1 ==$ nodeend) {// determines whether a node is single
$ Nodedate [$ i-1] [1] = '';
Continue;
} Else {
$ Nodedate [$ i-1] [1] = parseXMLNodes ($ xmlContent, $ assoc, $ attrassoc );
}
}
Return $ nodedate;
}
/* Parse the XML node attribute and return an array */
Function parseXMLAttrs ($ attrstr, $ assoc = null ){
If (! $ Attrstr) return array ();
Echo ($ attrstr. "n ");
$ Attrdata = array ();
$ Attrcenter = $ attrfoot = 1; // start conversion
$ Tmpnum = false;
While ($ attrcenter! = False ){
$ Tmpnum = strpos ($ attrstr, '= '');
$ Attrcenter = strpos ($ attrstr, '= "');
If ($ tmpnum = false ){
$ Attrfoot = strpos ($ attrstr, '"', $ attrcenter + 2 );
} Else {
$ Attrcenter = $ tmpnum;
$ Attrfoot = strpos ($ attrstr, ''', $ attrcenter + 2 );
}
If ($ assoc) {// whether to associate the array
$ Attrdata [substr ($ attrstr, 0, $ attrcenter)] = substr ($ attrstr, $ attrcenter + 2, $ attrfoot-$ attrcenter-2 );
} Else {
$ Attrdata [] = substr ($ attrstr, $ attrcenter + 2, $ attrfoot-$ attrcenter-2 );
}
$ Attrstr = trim (substr ($ attrstr, $ attrfoot + 1 ));
If (! $ Attrstr ){
Break;
}
}
Return $ attrdata;
}
// Parse the array as XML (not written)
Function parseArrayToXML ($ ary ){
}
// Parse the XML Nodes array (not written)
Function parseArrayToXMLNodes ($ ary ){
}
/* Test
$ T = microtime ();
// LoadXML ('../books. XML ');
Print_r (loadXML ('../test. XML', 1, 1); // you can find a common xml document.
Echo microtime ()-$ t;
*/
?>