getting the right XML tag in the parse.

This is a discussion on getting the right XML tag in the parse. within the PHP Language forums, part of the PHP Programming Forums category; I'm trying to parse an XML formatted post reply, and I can't figure out how to get the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-18-2008
news@celticbear.com
 
Posts: n/a
Default getting the right XML tag in the parse.

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;
}
Reply With Quote
  #2 (permalink)  
Old 07-18-2008
Michael Fesser
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

..oO(news@celticbear.com)

>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>


If this is the entire document, then it's not well-formed XML, because
there's no root element.

>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".


Of course you can access every element and every attribute in the tree.
Just use the right tools.

>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!
>[...]


You don't have to write your own parser. Use the SimpleXML extension
with some XPath expressions to get all the nodes you want.

Micha
Reply With Quote
  #3 (permalink)  
Old 07-18-2008
news@celticbear.com
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

On Jul 18, 10:51*am, Michael Fesser <neti...@gmx.de> wrote:
> .oO(n...@celticbear.com)
>
> >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>

>
> If this is the entire document, then it's not well-formed XML, because
> there's no root element.


LOL of course that's not the whole document, not the document at all.
It's an example specifically the nesting and tag labeling that I'm
talking about--for the sake of example.

> >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".

>
> Of course you can access every element and every attribute in the tree.
> Just use the right tools.


I knew it was possible...I was asking for advice HOW. WHAT are the
right tools?

> >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!
> >[...]

>
> You don't have to write your own parser. Use the SimpleXML extension
> with some XPath expressions to get all the nodes you want.


In my opinion, writing several lines of script in standard PHP is a
LOT easier than installing an extension (and learning how to install
an extension as I've never done that before.)
IF that's the ONLY way to do what I need to do, then so be it. I'll
figure out how to install SimpleXML and how to use XPath.
But if there's a way to do it by simply modifying the regular PHP
getElements and characterData, etc, that's the best way for me to go.
Reply With Quote
  #4 (permalink)  
Old 07-18-2008
Michael Fesser
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

..oO(news@celticbear.com)

>On Jul 18, 10:51*am, Michael Fesser <neti...@gmx.de> wrote:
>
>> >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".

>>
>> Of course you can access every element and every attribute in the tree.
>> Just use the right tools.

>
>I knew it was possible...I was asking for advice HOW. WHAT are the
>right tools?
>
>> >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!
>> >[...]

>>
>> You don't have to write your own parser. Use the SimpleXML extension
>> with some XPath expressions to get all the nodes you want.

>
>In my opinion, writing several lines of script in standard PHP is a
>LOT easier than installing an extension (and learning how to install
>an extension as I've never done that before.)


Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
standard extensions and should be installed and enabled by default in
every recent PHP. We are talking about PHP 5, right?

>IF that's the ONLY way to do what I need to do, then so be it. I'll
>figure out how to install SimpleXML and how to use XPath.


Check phpinfo() to see if SimpleXML is already there (it should be).
Then check the manual for some examples on how to use it with XPath
syntax to fetch arbitrary elements from an XML tree. Can't get any
easier.

Micha
Reply With Quote
  #5 (permalink)  
Old 07-18-2008
news@celticbear.com
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

On Jul 18, 2:29*pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(n...@celticbear.com)
>
>
>
> >On Jul 18, 10:51*am, Michael Fesser <neti...@gmx.de> wrote:

>
> >> >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".

>
> >> Of course you can access every element and every attribute in the tree..
> >> Just use the right tools.

>
> >I knew it was possible...I was asking for advice HOW. WHAT are the
> >right tools?

>
> >> >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!
> >> >[...]

>
> >> You don't have to write your own parser. Use the SimpleXML extension
> >> with some XPath expressions to get all the nodes you want.

>
> >In my opinion, writing several lines of script in standard PHP is a
> >LOT easier than installing an extension (and learning how to install
> >an extension as I've never done that before.)

>
> Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
> standard extensions and should be installed and enabled by default in
> every recent PHP. We are talking about PHP 5, right?
>
> >IF that's the ONLY way to do what I need to do, then so be it. I'll
> >figure out how to install SimpleXML and how to use XPath.

>
> Check phpinfo() to see if SimpleXML is already there (it should be).
> Then check the manual for some examples on how to use it with XPath
> syntax to fetch arbitrary elements from an XML tree. Can't get any
> easier.


Nope, PHP 4.3.9... there's a reason why our site can't use PHP5
yet...but I don't recall. One of the other developers are saying a
hundred pages will have to be rewritten before we can convert
otherwise it will completely mess up our e-commerce site.
So no, SimpleXML is not found in my phpinfo(). =/
Reply With Quote
  #6 (permalink)  
Old 07-18-2008
news@celticbear.com
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

On Jul 18, 2:47*pm, "n...@celticbear.com" <n...@celticbear.com> wrote:
> On Jul 18, 2:29*pm, Michael Fesser <neti...@gmx.de> wrote:
>
>
>
> > .oO(n...@celticbear.com)

>
> > >On Jul 18, 10:51*am, Michael Fesser <neti...@gmx.de> wrote:

>
> > >> >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".

>
> > >> Of course you can access every element and every attribute in the tree.
> > >> Just use the right tools.

>
> > >I knew it was possible...I was asking for advice HOW. WHAT are the
> > >right tools?

>
> > >> >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!
> > >> >[...]

>
> > >> You don't have to write your own parser. Use the SimpleXML extension
> > >> with some XPath expressions to get all the nodes you want.

>
> > >In my opinion, writing several lines of script in standard PHP is a
> > >LOT easier than installing an extension (and learning how to install
> > >an extension as I've never done that before.)

>
> > Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
> > standard extensions and should be installed and enabled by default in
> > every recent PHP. We are talking about PHP 5, right?

>
> > >IF that's the ONLY way to do what I need to do, then so be it. I'll
> > >figure out how to install SimpleXML and how to use XPath.

>
> > Check phpinfo() to see if SimpleXML is already there (it should be).
> > Then check the manual for some examples on how to use it with XPath
> > syntax to fetch arbitrary elements from an XML tree. Can't get any
> > easier.

>
> Nope, PHP 4.3.9... there's a reason why our site can't use PHP5
> yet...but I don't recall. One of the other developers are saying a
> hundred pages will have to be rewritten before we can convert
> otherwise it will completely mess up our e-commerce site.
> So no, SimpleXML is not found in my phpinfo(). =/


But it does have domxml, if that means anything:

domxml
DOM/XML enabled
DOM/XML API Version 20020815
libxml Version 20616
HTML Support enabled
XPath Support enabled
XPointer Support enabled
DOM/XSLT enabled
libxslt Version 1.1.11
libxslt compiled against libxml Version 2.6.16
DOM/EXSLT enabled
libexslt Version 1.1.11
Reply With Quote
  #7 (permalink)  
Old 07-18-2008
Paul Lautman
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

news@celticbear.com wrote:
> On Jul 18, 2:47 pm, "n...@celticbear.com" <n...@celticbear.com> wrote:
>> On Jul 18, 2:29 pm, Michael Fesser <neti...@gmx.de> wrote:
>>
>>
>>
>> > .oO(n...@celticbear.com)

>>
>> > >On Jul 18, 10:51 am, Michael Fesser <neti...@gmx.de> wrote:

>>
>> > >> >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".

>>
>> > >> Of course you can access every element and every attribute in
>> > >> the tree. Just use the right tools.

>>
>> > >I knew it was possible...I was asking for advice HOW. WHAT are the
>> > >right tools?

>>
>> > >> >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!
>> > >> >[...]

>>
>> > >> You don't have to write your own parser. Use the SimpleXML
>> > >> extension with some XPath expressions to get all the nodes you
>> > >> want.

>>
>> > >In my opinion, writing several lines of script in standard PHP is
>> > >a LOT easier than installing an extension (and learning how to
>> > >install an extension as I've never done that before.)

>>
>> > Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
>> > standard extensions and should be installed and enabled by default
>> > in every recent PHP. We are talking about PHP 5, right?

>>
>> > >IF that's the ONLY way to do what I need to do, then so be it.
>> > >I'll figure out how to install SimpleXML and how to use XPath.

>>
>> > Check phpinfo() to see if SimpleXML is already there (it should
>> > be). Then check the manual for some examples on how to use it with
>> > XPath syntax to fetch arbitrary elements from an XML tree. Can't
>> > get any easier.

>>
>> Nope, PHP 4.3.9... there's a reason why our site can't use PHP5
>> yet...but I don't recall. One of the other developers are saying a
>> hundred pages will have to be rewritten before we can convert
>> otherwise it will completely mess up our e-commerce site.
>> So no, SimpleXML is not found in my phpinfo(). =/


How about: http://www.phpclasses.org/browse/file/22596.html
no extensions to load.


Reply With Quote
  #8 (permalink)  
Old 07-18-2008
cwdjrxyz
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

On Jul 18, 2:47*pm, "n...@celticbear.com" <n...@celticbear.com> wrote:
> On Jul 18, 2:29*pm, Michael Fesser <neti...@gmx.de> wrote:
>
>
>
> > .oO(n...@celticbear.com)

>
> > >On Jul 18, 10:51*am, Michael Fesser <neti...@gmx.de> wrote:

>
> > >> >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".

>
> > >> Of course you can access every element and every attribute in the tree.
> > >> Just use the right tools.

>
> > >I knew it was possible...I was asking for advice HOW. WHAT are the
> > >right tools?

>
> > >> >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!
> > >> >[...]

>
> > >> You don't have to write your own parser. Use the SimpleXML extension
> > >> with some XPath expressions to get all the nodes you want.

>
> > >In my opinion, writing several lines of script in standard PHP is a
> > >LOT easier than installing an extension (and learning how to install
> > >an extension as I've never done that before.)

>
> > Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
> > standard extensions and should be installed and enabled by default in
> > every recent PHP. We are talking about PHP 5, right?

>
> > >IF that's the ONLY way to do what I need to do, then so be it. I'll
> > >figure out how to install SimpleXML and how to use XPath.

>
> > Check phpinfo() to see if SimpleXML is already there (it should be).
> > Then check the manual for some examples on how to use it with XPath
> > syntax to fetch arbitrary elements from an XML tree. Can't get any
> > easier.

>
> Nope, PHP 4.3.9... there's a reason why our site can't use PHP5
> yet...but I don't recall. One of the other developers are saying a
> hundred pages will have to be rewritten before we can convert
> otherwise it will completely mess up our e-commerce site.
> So no, SimpleXML is not found in my phpinfo(). =/


If I remember correctly, it sometimes is possible to have 2 versions
of php on a server. In your case that would mean keeping your current
version of 4 as primary for the extension .php and using the
extension .php5 for the new 5 version. If that can be done and you can
find someone to do it for your server, then you perhaps could keep all
happy. I recently moved my two domains and a subdomain from an old
server that supported php 4 only to a new server that supports php 5
only. There were only a very few minor problems with any of my pages,
and no problems with the php upgrade. Of course some others may have
written code, some of which even predates php4, that causes problems
on an upgrade to php5.
Reply With Quote
  #9 (permalink)  
Old 07-18-2008
news@celticbear.com
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

On Jul 18, 3:47*pm, cwdjrxyz <spamtr...@cwdjr.info> wrote:
> On Jul 18, 2:47*pm, "n...@celticbear.com" <n...@celticbear.com> wrote:
>
>
>
> > On Jul 18, 2:29*pm, Michael Fesser <neti...@gmx.de> wrote:

>
> > > .oO(n...@celticbear.com)

>
> > > >On Jul 18, 10:51*am, Michael Fesser <neti...@gmx.de> wrote:

>
> > > >> >I need the value of A's "foo", not B's "foo". But from what I canfind
> > > >> >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".

>
> > > >> Of course you can access every element and every attribute in the tree.
> > > >> Just use the right tools.

>
> > > >I knew it was possible...I was asking for advice HOW. WHAT are the
> > > >right tools?

>
> > > >> >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!
> > > >> >[...]

>
> > > >> You don't have to write your own parser. Use the SimpleXML extension
> > > >> with some XPath expressions to get all the nodes you want.

>
> > > >In my opinion, writing several lines of script in standard PHP is a
> > > >LOT easier than installing an extension (and learning how to install
> > > >an extension as I've never done that before.)

>
> > > Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
> > > standard extensions and should be installed and enabled by default in
> > > every recent PHP. We are talking about PHP 5, right?

>
> > > >IF that's the ONLY way to do what I need to do, then so be it. I'll
> > > >figure out how to install SimpleXML and how to use XPath.

>
> > > Check phpinfo() to see if SimpleXML is already there (it should be).
> > > Then check the manual for some examples on how to use it with XPath
> > > syntax to fetch arbitrary elements from an XML tree. Can't get any
> > > easier.

>
> > Nope, PHP 4.3.9... there's a reason why our site can't use PHP5
> > yet...but I don't recall. One of the other developers are saying a
> > hundred pages will have to be rewritten before we can convert
> > otherwise it will completely mess up our e-commerce site.
> > So no, SimpleXML is not found in my phpinfo(). =/

>
> If I remember correctly, it sometimes is possible to have 2 versions
> of php on a server. In your case that would mean keeping your current
> version of 4 as primary for the extension .php and using the
> extension .php5 for the new 5 version. If that can be done and you can
> find someone to do it for your server, then you perhaps could keep all
> happy. I recently moved my two domains and a subdomain from an old
> server that supported php 4 only to a new server that supports php 5
> only. There were only a very few minor problems with any of my pages,
> and no problems with the php upgrade. Of course some others may have
> written code, some of which even predates php4, that causes problems
> on an upgrade to php5.


Hmm, cool. Thanks for the reply! I'll check into that.
-Liam
Reply With Quote
  #10 (permalink)  
Old 07-19-2008
Jerry Stuckle
 
Posts: n/a
Default Re: getting the right XML tag in the parse.

news@celticbear.com wrote:
> On Jul 18, 3:47 pm, cwdjrxyz <spamtr...@cwdjr.info> wrote:
>> On Jul 18, 2:47 pm, "n...@celticbear.com" <n...@celticbear.com> wrote:
>>
>>
>>
>>> On Jul 18, 2:29 pm, Michael Fesser <neti...@gmx.de> wrote:
>>>> .oO(n...@celticbear.com)
>>>>> On Jul 18, 10:51 am, Michael Fesser <neti...@gmx.de> wrote:
>>>>>>> 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".
>>>>>> Of course you can access every element and every attribute in the tree.
>>>>>> Just use the right tools.
>>>>> I knew it was possible...I was asking for advice HOW. WHAT are the
>>>>> right tools?
>>>>>>> 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!
>>>>>>> [...]
>>>>>> You don't have to write your own parser. Use the SimpleXML extension
>>>>>> with some XPath expressions to get all the nodes you want.
>>>>> In my opinion, writing several lines of script in standard PHP is a
>>>>> LOT easier than installing an extension (and learning how to install
>>>>> an extension as I've never done that before.)
>>>> Why re-invent the wheel? SimpleXML (and the more powerful DOM) are
>>>> standard extensions and should be installed and enabled by default in
>>>> every recent PHP. We are talking about PHP 5, right?
>>>>> IF that's the ONLY way to do what I need to do, then so be it. I'll
>>>>> figure out how to install SimpleXML and how to use XPath.
>>>> Check phpinfo() to see if SimpleXML is already there (it should be).
>>>> Then check the manual for some examples on how to use it with XPath
>>>> syntax to fetch arbitrary elements from an XML tree. Can't get any
>>>> easier.
>>> Nope, PHP 4.3.9... there's a reason why our site can't use PHP5
>>> yet...but I don't recall. One of the other developers are saying a
>>> hundred pages will have to be rewritten before we can convert
>>> otherwise it will completely mess up our e-commerce site.
>>> So no, SimpleXML is not found in my phpinfo(). =/

>> If I remember correctly, it sometimes is possible to have 2 versions
>> of php on a server. In your case that would mean keeping your current
>> version of 4 as primary for the extension .php and using the
>> extension .php5 for the new 5 version. If that can be done and you can
>> find someone to do it for your server, then you perhaps could keep all
>> happy. I recently moved my two domains and a subdomain from an old
>> server that supported php 4 only to a new server that supports php 5
>> only. There were only a very few minor problems with any of my pages,
>> and no problems with the php upgrade. Of course some others may have
>> written code, some of which even predates php4, that causes problems
>> on an upgrade to php5.

>
> Hmm, cool. Thanks for the reply! I'll check into that.
> -Liam
>


Yes, you need to get on PHP5 ASAP. PHP4 is at end of life - there will
not be any patches any more for it - not even security patches. So
you're running a completely unsupported product.

Not to say there hasn't been warning - about 2 years worth.

But virtually everything I've ever had on PHP4 runs fine on PHP5. If
your have a lot of pages which don't work right, chances are someone was
doing something rather flaky in the first place. There just aren't THAT
many incompatibilities between the two.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 03:44 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0