在解析中获取正确的XML标记。 [英] getting the right XML tag in the parse.

查看:70
本文介绍了在解析中获取正确的XML标记。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析XML格式的帖子回复,但我无法确定如何获得正确标记的值。

XML的格式如下:


< A>

< foo> val1< / foo>

< / A>

< B>

< foo> val2< / foo>

< / B>

< BAR>

val3

< / BAR>


我需要A'的价值foo,而不是B'的foo。但是我能从中找到

我只能获得嵌套中嵌套标记最低的值。即:

foo或BAR。不是特别是A或B'的foo。

下面是我正在使用的脚本。在这种情况下,TOTALCHARGES是

" foo"我正在寻找,但我需要它专门获得A'而不是

B'的。

如果有人可以指出我在哪里可以找出方法,我会很感激

it!

-Liam


//定义startElement函数

函数startElement($ xml_parser,$ element_name,$ element_attrs){

global $ printout,$ tag,$ output1;


switch($ element_name){

case" TOTALCHARGES":

$ printout = true;

$ tag =" TOTALCHARGES";

休息;

}

}


//定义endElement函数

函数endElement($ xml_parser,$ element_name){

global $ printout,$ tag,$ output1;


switch($ element_name){

case" TOTALCHARGES":

$ printout = false;

$ tag ="" ;;

break;

}

}


//定义characterData函数

函数characterData($ xml_parser,$ fp){

global $ printout,$ tag,$ output1;


if(($ tag ==" TOTALCHARGES" )&& ($ printout == true)){

$ output1 = $ fp;

// echo $ output;

}

返回$ output1;

}

I''m trying to parse an XML formatted post reply, and I can''t figure
out how to get the value of the right tag.
The XML is formatted like this:

<A>
<foo>val1</foo>
</A>
<B>
<foo>val2</foo>
</B>
<BAR>
val3
</BAR>

I need the value of A''s "foo", not B''s "foo". But from what I can find
I can only get the value of the lowest nested tag in a nest. i.e.:
"foo" or "BAR". Not specifically A or B''s "foo".
Below is the script I''m using. In this case "TOTALCHARGES" is the
"foo" I''m looking for, but I need it to specifically get A''s and not
B''s.
If someone can point me to where I can find out how, I''d appreciate
it!
-Liam

//Defines startElement function
function startElement($xml_parser, $element_name, $element_attrs) {
global $printout, $tag, $output1;

switch($element_name) {
case "TOTALCHARGES":
$printout = true;
$tag = "TOTALCHARGES";
break;
}
}

//Defines endElement function
function endElement($xml_parser, $element_name) {
global $printout, $tag, $output1;

switch($element_name) {
case "TOTALCHARGES":
$printout = false;
$tag = "";
break;
}
}

//Defines characterData function
function characterData($xml_parser, $fp) {
global $printout, $tag, $output1;

if (($tag=="TOTALCHARGES") && ($printout==true)) {
$output1 = $fp;
//echo $output;
}
return $output1;
}

推荐答案

xml_parser,
xml_parser,


element_name,
element_name,


element_attrs){

global
element_attrs) {
global


这篇关于在解析中获取正确的XML标记。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆