-
- $simple = "simple note";
- $p = xml_parser_create();
- xml_parse_into_struct($p, $simple, $vals, $index);
- xml_parser_free($p);
- echo "Index arrayn";
- print_r($index);
- echo "nVals arrayn";
- print_r($vals);
- ?>
-
复制代码
输出:
-
-
$xml = '
- note1extra1
- note2
- note3extra3
- ';
$p = xml_parser_create();
- xml_parse_into_struct($p, $xml, $values, $tags);
- xml_parser_free($p);
- $result = array();
- //下面的遍历方式有bug隐患
- for ($i=0; $i<3; $i++) {
- $result[$i] = array();
- $result[$i]["note"] = $values[$tags["NOTE"][$i]]["value"];
- $result[$i]["extra"] = $values[$tags["EXTRA"][$i]]["value"];
- }
- print_r($result);
- ?>
-
复制代码
要是按照上面那种方式遍历,会得到错误的结果(extra3跑到第二个para里了)。所以要以一种比较严谨的方式遍历:
-
-
- $result = array();
- $paraTagIndexes = $tags['PARA'];
- $paraCount = count($paraTagIndexes);
- for($i = 0; $i < $paraCount; $i += 2) {
- $para = array();
- //遍历para标签对之间的所有值
- for($j = $paraTagIndexes[$i]; $j < $paraTagIndexes[$i+1]; $j++) {
- $value = $values[$j]['value'];
- if(empty($value)) continue;
$tagname = strtolower($values[$j]['tag']);
- if(in_array($tagname, array('note','extra'))) {
- $para[$tagname] = $value;
- }
- }
- $result[] = $para;
- }
- ?>
-
-
复制代码
其实,平时很少用xml_parse_into_struct函数,所以上面所谓“严谨”的代码保不齐还会有其他情况下的bug。
xml_set_element_handler
这种方式是为parser设置处理元素起始、元素终止的回调函数。
配套的还有xml_set_character_data_handler用来为parser设置数据的回调函数。
这种方式写的代码比较清晰,利于维护。
例子:
-
-
$xml = <<
- note1extra1
- note2
- note3extra3
- XML;
$result = array();
- $index = -1;
- $currData;
function charactor($parser, $data) {
- global $currData;
- $currData = $data;
- }
function startElement($parser, $name, $attribs) {
- global $result, $index;
- $name = strtolower($name);
- if($name == 'para') {
- $index++;
- $result[$index] = array();
- }
- }
function endElement($parser, $name) {
- global $result, $index, $currData;
- $name = strtolower($name);
- if($name == 'note' || $name == 'extra') {
- $result[$index][$name] = $currData;
- }
- }
$xml_parser = xml_parser_create();
- xml_set_character_data_handler($xml_parser, "charactor");
- xml_set_element_handler($xml_parser, "startElement", "endElement");
- if (!xml_parse($xml_parser, $xml)) {
- echo "Error when parse xml: ";
- echo xml_error_string(xml_get_error_code($xml_parser));
- }
- xml_parser_free($xml_parser);
print_r($result);
- ?>
-
复制代码
It can be seen that although the set handler method has more lines of code, it has clear ideas and better readability, but its performance is slightly slower than the first method and its flexibility is not strong. XML Parser supports PHP4 and is suitable for systems using older versions. For PHP5 environment, give priority to the following method.
2,SimpleXML
SimpleXML is a set of simple and easy-to-use XML tools provided after PHP5. It can convert XML into objects that are convenient for processing, and can also organize and generate XML data. However, it does not apply to xml containing namespace, and the xml must be well-formed. It provides three methods: simplexml_import_dom, simplexml_load_file, simplexml_load_string. The function name intuitively explains the function. All three functions return SimpleXMLElement objects, and data is read/added through SimpleXMLElement operations.
-
-
$string = <<
- login< ;/cmd>
- imdonkey
- XML;
$xml = simplexml_load_string($string);
- print_r($xml);
- $login = $xml->login;//What is returned here is still a SimpleXMLElement object
- print_r($login);
- $login = (string) $xml->login;//When doing data comparison, pay attention to First force conversion
- print_r($login);
- ?>
-
Copy code
The advantage of SimpleXML is that it is simple to develop, but the disadvantage is that it will load the entire xml into the memory before processing. Therefore, it may be difficult to parse an xml document with a lot of content. If you are reading small files and the xml does not contain namespace, then SimpleXML is a good choice.
3,XMLReader
XMLReader is also an extension after PHP5 (installed by default after 5.1). It moves in the document flow like a cursor and stops at each node. It is very flexible to operate. It provides fast and non-cached streaming access to input, and can read a stream or document, allowing the user to extract data from it, and skip records that are not meaningful to the application. 1 2 Next Page Last Page
|