Previously, simplexml of php was used to parse xml for Project catch-up. at that time, simplexml was not supported! [CDATA tag, all values in this tag cannot be obtained from a CDATA converter found online at the time. after modification, filter out the CDATA tag. As follows:
The code is as follows:
// States:
//
// 'Out'
// '<'
//' //' //' //' //' //' // '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' If ('D' = $ v ){
$ State = $ state. $ v;
} Else {
$ New_xml. = $ state. $ v;
$ State = 'out ';
}
Break;
Case' If ('a' = $ v ){
$ State = $ state. $ v;
} Else {
$ New_xml. = $ state. $ v;
$ State = 'out ';
}
Break;
Case' If ('t' = $ v ){
$ State = $ state. $ v;
} Else {
$ New_xml. = $ state. $ v;
$ State = 'out ';
}
Break;
Case' If ('a' = $ v ){
$ State = $ state. $ v;
} Else {
$ New_xml. = $ state. $ v;
$ State = 'out ';
}
Break;
Case' 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, it was found that alert always came out, saying that simplexml parsing is incorrect.
It is found that the original xml data isdomain[test]]] >. 出现了连续的3个], 造成上面的解析函数不能处理。 <br><br>而且这个问题很难修正, 你不知道下次会不会有4, 5个]出现。 <br><br>所以决定还是将这段解析 的代码换成DOM XML,本身 DOM的处理还是比较简单的, <br><br>包含DOMElement, DOMDocument, DOMNodeList, DOMNode几个 component. <br><br>对于 DOMNode有nodeValue, nodeType, nodeName的成员函数。 <br><br>首先先用loadXML将string转化为DOMDocument对像, 再用getElementsByTagName转化为DOMNodeList对像, 再使用->item(0)转化为DOMNOde, 然后就可以使用上面的三种方法了。 <br><br>对于 test</aa>这种xml标签, 要使用 attribute函数。
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.