A CDATA Converter was found on the Internet, and after modification, the CDATA label was filtered out. As follows
Copy Code code as follows:
States:
//
' Out '
' < '
' <! '
' <! ['
' <! C
' <! [CD '
' <! [Cdat '
' <! [CDATA '
' In '
// ']'
// ']]'
//
(Yes, the states a represented by strings.)
//
$state = ' out ';
$a = Str_split ($xml);
$new _xml = ';
foreach ($a as $k => $v) {
Deal with ' state '.
Switch ($state) {
Case ' out ':
if (' < ' = $v) {
$state = $v;
} else {
$new _xml. = $v;
}
Break
Case ' < ':
if ('! ' = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! ':
if (' [' = = $v ') {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [':
if (' C ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [C ':
if (' D ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [CD ':
if (' A ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [CDA ':
if (' T ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [Cdat ':
if (' A ' = = $v) {
$state = $state. $v;
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' <! [CDATA ':
if (' [' = = $v ') {
$cdata = ';
$state = ' in ';
} else {
$new _xml. = $state. $v;
$state = ' out ';
}
Break
Case ' in ':
if ('] ' = = $v) {
$state = $v;
} else {
$cdata. = $v;
}
Break
Case '] ':
if ('] ' = = $v) {
$state = $state. $v;
} else {
$cdata. = $state. $v;
$state = ' in ';
}
Break
Case ']] ':
if (' > ' = $v) {
$new _xml. = Htmlentities ($cdata);
# $new _xml.= $cdata;
$new _xml. = Str_replace (' > ', ' > ',
Str_replace (' > ', ' < ',
Str_replace (' "', '", "),
Str_replace (' & ', ' & ',
$cdata))));
$state = ' out ';
} else {
$cdata. = $state. $v;
$state = ' in ';
}
Break
}//Switch
}
//
return.
//
return $new _xml;
Recently found that there is always alert sent to say that SimpleXML parsing error.
found that the original XML data is <! [Cdata[domain[test]]]. There are 3 consecutive occurrences, resulting in the above parsing function not being processed.
And the problem is hard to fix, and you don't know if there will be 4 or 5 next time.
So decide to change this parsing code into DOM XML, the processing of the DOM itself is relatively simple,
Contains DomElement, DOMDocument, Domnodelist, domnode several component.
For Domnode there are nodevalue, NodeType, and nodename member functions.
First, we use Loadxml to convert string to DOMDocument, then convert getElementsByTagName to domnodelist image, then use->item (0) to convert to Domnode, Then you can use the three methods above.
For <aa color= ' red ' >test</aa> this XML tag, use the attribute function.