其实如果
<root><mzpurchase-verification>
<result>OK</result>
<mzTxnId>mzTxnId</mzTxnId>
<extTxnId>extTxnId</extTxnId>
<balance>i</balance>
<currency>currency</currency>
<auth>auth</auth>
</mzpurchase-verification><mzpurchase-verification>
<result>OK</result>
<mzTxnId>mzTxnId</mzTxnId>
<extTxnId>extTxnId</extTxnId>
<balance>i</balance>
<currency>currency</currency>
<auth>auth</auth>
</mzpurchase-verification>
......
</root>这样的格式是固定的话,完全可以写一个专门的字符串处理函数来处理...是固定吗?
<root><mzpurchase-verification>
<result>OK</result>
<mzTxnId>mzTxnId</mzTxnId>
<extTxnId>extTxnId</extTxnId>
<balance>i</balance>
<currency>currency</currency>
<auth>auth</auth>
</mzpurchase-verification><mzpurchase-verification>
<result>OK</result>
<mzTxnId>mzTxnId</mzTxnId>
<extTxnId>extTxnId</extTxnId>
<balance>i</balance>
<currency>currency</currency>
<auth>auth</auth>
</mzpurchase-verification>
......
</root>这样的格式是固定的话,完全可以写一个专门的字符串处理函数来处理...是固定吗?
里面的zxxml
参考sample13
foreach($vals as $key=>$val){
if ($key == "mzpurchase-response") {
$result = $vals[1][value]."<br>";
$mzTxnId = $vals[2][value]."<br>";
$extTxnId = $vals[3][value]."<br>";
$balance = $vals[4][value]."<br>";
$currency = $vals[5][value]."<br>";
$auth = $vals[6][value]."<br>";
echo $result;
echo $mzTxnId;
echo $extTxnId;
echo $balance;
echo $currency;
echo $auth;
}
}
假设最外层数组叫$arr1
foreach($arr1 as $value){
foreach($value as $subname){
if($subname['leval']==3){
//you code;
echo $subname['value'];
}
}
}可能不太对
我对php不熟悉
<?php
$xml =<<< XML
<root>
<mzpurchase-verification>
<result>OK</result>
<mzTxnId>mzTxnId</mzTxnId>
<extTxnId>extTxnId</extTxnId>
<balance>i</balance>
<currency>currency</currency>
<auth>auth</auth>
</mzpurchase-verification>
<mzpurchase-verification>
<result>OK</result>
<mzTxnId>mzTxnId</mzTxnId>
<extTxnId>extTxnId</extTxnId>
<balance>i</balance>
<currency>currency</currency>
<auth>auth</auth>
</mzpurchase-verification>
</root>
XML;$depth = array();
$temp = array();
$tag_name = "";
$mode = false;
$tag = "mzpurchase-verification";function startElement($parser, $name, $attrs) {
global $depth,$temp,$tag,$tag_name,$mode;
if(strtolower($name) == strtolower($tag)) {
$temp = array();
$mode = true;
}
$tag_name = $name;
}function endElement($parser, $name) {
global $depth,$temp,$tag,$mode;
if(strtolower($name) == strtolower($tag)) {
$depth[] = $temp;
$mode = false;
}
}function characterData($parser, $data) {
global $depth,$temp,$tag,$tag_name,$mode;
if($mode && strtolower($tag_name) != strtolower($tag))
$temp[$tag_name] .= $data;
}$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");//if (!($fp = fopen($file, "r"))) {
// die("could not open XML input");
//}//while ($data = fread($fp, 4096)) {
// if (!xml_parse($xml_parser, $data, feof($fp))) {
if (!xml_parse($xml_parser, $xml, true)) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
//}
xml_parser_free($xml_parser);
print_r($depth);
?>
<?php
$ar = array(
array(tag => 'ROOT', type => 'open', level => 1),
array(tag => 'MZPURCHASE-VERIFICATION', type => 'open', level => 2),
array(tag => 'RESULT', type => 'complete', level => 3, value => 'OK'),
array(tag => 'MZTXNID', type => 'complete', level => 3, value => 'mzTxnId'),
array(tag => 'EXTTXNID', type => 'complete', level => 3),
array(tag => 'BALANCE', type => 'complete', level => 3, value => 1),
array(tag => 'CURRENCY', type => 'complete', level => 3),
array(tag => 'AUTH', type => 'complete', level => 3),
array(tag => 'MZPURCHASE-VERIFICATION', type => 'close', level => 2),
array(tag => 'MZPURCHASE-VERIFICATION', type => 'open', level => 2),
array(tag => 'RESULT', type => 'complete', level => 3, value => 'OK'),
array(tag => 'MZTXNID', type => 'complete', level => 3, value => 'mzTxnId'),
array(tag => 'EXTTXNID', type => 'complete', level => 3),
array(tag => 'BALANCE', type => 'complete', level => 3, value => 2),
array(tag => 'CURRENCY', type => 'complete', level => 3),
array(tag => 'AUTH', type => 'complete', level => 3),
array(tag => 'MZPURCHASE-VERIFICATION', type => 'close', level => 2),
array(tag => 'MZPURCHASE-VERIFICATION', type => 'open', level => 2),
array(tag => 'RESULT', type => 'complete', level => 3, value => 'OK'),
array(tag => 'MZTXNID', type => 'complete', level => 3, value => 'mzTxnId'),
array(tag => 'EXTTXNID', type => 'complete', level => 3),
array(tag => 'BALANCE', type => 'complete', level => 3, value => 3),
array(tag => 'CURRENCY', type => 'complete', level => 3),
array(tag => 'AUTH', type => 'complete', level => 3),
array(tag => 'MZPURCHASE-VERIFICATION', type => 'close', level => 2),
array(tag => 'ROOT', type => 'close', level => 1),
);
$tag = 'MZPURCHASE-VERIFICATION';
$mode = false;
foreach($ar as $v) {
if($v[tag] == $tag && $v[type] == 'open') {
$mode = true;
$temp = array();
}else if($v[tag] == $tag && $v[type] == 'close') {
$mode = false;
$out[] = $temp;
}else if($v[value])
$temp[$v[tag]] = $v[value];
}
print_r($out);
?>
结果
Array
(
[0] => Array
(
[RESULT] => OK
[MZTXNID] => mzTxnId
[BALANCE] => 1
) [1] => Array
(
[RESULT] => OK
[MZTXNID] => mzTxnId
[BALANCE] => 2
) [2] => Array
(
[RESULT] => OK
[MZTXNID] => mzTxnId
[BALANCE] => 3
))