XML parsing help

This is a discussion on XML parsing help within the PHP Language forums, part of the PHP Programming Forums category; Newbie alert: I am trying to parse an XML string using simplexml_load_string and am really lost. I don't see ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-24-2004
Gary Quiring
 
Posts: n/a
Default XML parsing help

Newbie alert:
I am trying to parse an XML string using simplexml_load_string and am
really lost. I don't see the data using an example like below:

<DATA><ACCT>12345</ACCT><ITEMS><ITEM>ABC</ITEM><ITEM>XYZ<ITEM><ITEMS></DATA>

$string=file_get_contents('xml5');
$stockchk = simplexml_load_string($string);

How do I reference the data in $stockchk? I tried $stockchk->ACCT and
I don't get any data.

Thanks
Gary Quiring

Reply With Quote
  #2 (permalink)  
Old 12-24-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: XML parsing help

Gary Quiring wrote:
> Newbie alert:
> I am trying to parse an XML string using simplexml_load_string and am
> really lost. I don't see the data using an example like below:
>
> <DATA><ACCT>12345</ACCT><ITEMS><ITEM>ABC</ITEM><ITEM>XYZ<ITEM><ITEMS></DATA>
>
> $string=file_get_contents('xml5');
> $stockchk = simplexml_load_string($string);
>
> How do I reference the data in $stockchk? I tried $stockchk->ACCT and
> I don't get any data.
>


That's because the XML you are using is invalid because it contains unclosed
tags. Below is a valid version:

<DATA><ACCT>12345</ACCT><ITEMS><ITEM>ABC</ITEM><ITEM>XYZ</ITEM></ITEMS></DATA>

Notice the differences.


JW



Reply With Quote
  #3 (permalink)  
Old 12-25-2004
Gary Quiring
 
Posts: n/a
Default Re: XML parsing help

On Fri, 24 Dec 2004 21:49:07 +0100, "Janwillem Borleffs"
<jw@jwscripts.com> wrote:

><DATA><ACCT>12345</ACCT><ITEMS><ITEM>ABC</ITEM><ITEM>XYZ</ITEM></ITEMS></DATA>

My example was poorly entered into the post. My actual XML file is
formatted correctly. I still do not know how to reference the values.

Thanks
Gary

Reply With Quote
  #4 (permalink)  
Old 12-25-2004
Gary Quiring
 
Posts: n/a
Default Re: XML parsing help

On Fri, 24 Dec 2004 19:47:57 -0500, Gary Quiring <gquiring@msn.com>
wrote:

>On Fri, 24 Dec 2004 21:49:07 +0100, "Janwillem Borleffs"
><jw@jwscripts.com> wrote:
>
>><DATA><ACCT>12345</ACCT><ITEMS><ITEM>ABC</ITEM><ITEM>XYZ</ITEM></ITEMS></DATA>

>My example was poorly entered into the post. My actual XML file is
>formatted correctly. I still do not know how to reference the values.
>
>Thanks
>Gary

If I do a vardump I do see everything:
object(SimpleXMLElement)#1 (2) { ["ACCT"]=> string(5) "12345"
["ITEMS"]=> object(SimpleXMLElement)#2 (1) { ["ITEM"]=> array(2) {
[0]=> string(3) "ABC" [1]=> string(3) "XYZ" } } }

But I do not understand how do I know there are two items to grab? If
I use echo "<BR>$xml->ITEM<BR>"; I see do not get the values. I also
tried:
for each ($ITEM as $STUFF)
{
echo $STUFF;
}


Reply With Quote
  #5 (permalink)  
Old 12-25-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: XML parsing help

Gary Quiring wrote:
> But I do not understand how do I know there are two items to grab? If
> I use echo "<BR>$xml->ITEM<BR>"; I see do not get the values. I also
> tried:


This fails because you are not following the structure of the XML. In the
XML the ITEM elements are children of the ITEMS element.

To print all the ITEM elements, you should do as follows:

foreach ($stockchk->ITEMS->ITEM as $item) {
echo $item, "<br />";
}

Or to print the first element only, either:

print $stockchk->ITEMS->ITEM;

Or:

print $stockchk->ITEMS->ITEM[1];

Print the second element:

print $stockchk->ITEMS->ITEM[2];


JW



Reply With Quote
  #6 (permalink)  
Old 12-25-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: XML parsing help

Gary Quiring wrote:
> But I do not understand how do I know there are two items to grab? If
> I use echo "<BR>$xml->ITEM<BR>"; I see do not get the values. I also
> tried:


This fails because you are not following the structure of the XML. In the
XML the ITEM elements are children of the ITEMS element.

To print all the ITEM elements, you should do as follows:

foreach ($stockchk->ITEMS->ITEM as $item) {
echo $item, "<br />";
}

Or to print the first element only, either:

print $stockchk->ITEMS->ITEM;

Or:

print $stockchk->ITEMS->ITEM[1];

Print the second element:

print $stockchk->ITEMS->ITEM[2];


JW



Reply With Quote
  #7 (permalink)  
Old 12-28-2004
Gary Quiring
 
Posts: n/a
Default Re: XML parsing help

On Sat, 25 Dec 2004 02:42:40 +0100, "Janwillem Borleffs" <jw@jwscripts.com>
wrote:

>Gary Quiring wrote:
>> But I do not understand how do I know there are two items to grab? If
>> I use echo "<BR>$xml->ITEM<BR>"; I see do not get the values. I also
>> tried:

>
>This fails because you are not following the structure of the XML. In the
>XML the ITEM elements are children of the ITEMS element.
>
>To print all the ITEM elements, you should do as follows:
>
>foreach ($stockchk->ITEMS->ITEM as $item) {
> echo $item, "<br />";
>}
>
>Or to print the first element only, either:
>
>print $stockchk->ITEMS->ITEM;
>
>Or:
>
>print $stockchk->ITEMS->ITEM[1];
>
>Print the second element:
>
>print $stockchk->ITEMS->ITEM[2];
>
>
>JW
>
>

Thank you JW. Your info was very helpful.

Gary

Reply With Quote
  #8 (permalink)  
Old 12-29-2004
Gary Quiring
 
Posts: n/a
Default Re: XML parsing help

On Sat, 25 Dec 2004 02:42:40 +0100, "Janwillem Borleffs" <jw@jwscripts.com>
wrote:

>Gary Quiring wrote:
>> But I do not understand how do I know there are two items to grab? If
>> I use echo "<BR>$xml->ITEM<BR>"; I see do not get the values. I also
>> tried:

>
>This fails because you are not following the structure of the XML. In the
>XML the ITEM elements are children of the ITEMS element.
>
>To print all the ITEM elements, you should do as follows:
>
>foreach ($stockchk->ITEMS->ITEM as $item) {
> echo $item, "<br />";
>}
>
>Or to print the first element only, either:
>
>print $stockchk->ITEMS->ITEM;
>
>Or:
>
>print $stockchk->ITEMS->ITEM[1];
>
>Print the second element:
>
>print $stockchk->ITEMS->ITEM[2];
>
>
>JW
>
>

Is there a function call to get a count of all the elements in ITEM?
Reply With Quote
  #9 (permalink)  
Old 12-30-2004
Janwillem Borleffs
 
Posts: n/a
Default Re: XML parsing help

Gary Quiring wrote:
> Is there a function call to get a count of all the elements in ITEM?


Since neither a function nor a property appears to exist, you could use the
following workaround:

$length = count($stockchk->xpath('//ITEMS/ITEM'));


JW



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 09:46 AM.


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