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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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; } |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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? |
|
|||
|
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 |