473,505 Members | 15,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bizzare array structure from xml_parse_into_struct()

I wanted to test xml_parse_into_struct() so I took the example off of
www.php.net and put this code up on a site:

<?php
$simple = <<<END
<item>
<cbId>345</cbId>
<cbHeadline>Hey there</cbHeadline>
<cbMainContent>God is not dead, he is merely
sleeping.</cbMainContent>
</item>
<item>
<cbId>432</cbId>
<cbHeadline>What a dog, what a dog</cbHeadline>
<cbMainContent>In the manger.</cbMainContent>
</item>
END;

$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
?>

However, what I got back made no sense to me. Only the first ITEM made
it into the arrays, and all the other tags somehow got counted as
ITEMS. Why?

If I simply want my XML code turned into a two dimensional array, what
is the simplest way to do it? Is xml_parse_into_struct working
correctly, and I simply misunderstand it, or is there another way for
me to do this?

Index array
Array
(
[ITEM] => Array
(
[0] => 0
[1] => 2
[2] => 3
[3] => 5
[4] => 6
[5] => 8
[6] => 9
)

[CBID] => Array
(
[0] => 1
)

[CBHEADLINE] => Array
(
[0] => 4
)

[CBMAINCONTENT] => Array
(
[0] => 7
)

)

Vals array
Array
(
[0] => Array
(
[tag] => ITEM
[type] => open
[level] => 1
[value] =>

)

[1] => Array
(
[tag] => CBID
[type] => complete
[level] => 2
[value] => 345
)

[2] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 1
)

[3] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)

[4] => Array
(
[tag] => CBHEADLINE
[type] => complete
[level] => 2
[value] => Hey there
)

[5] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 1
)

[6] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 1
)

[7] => Array
(
[tag] => CBMAINCONTENT
[type] => complete
[level] => 2
[value] => God is not dead, he is merely sleeping.
)

[8] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 1
)

[9] => Array
(
[tag] => ITEM
[type] => close
[level] => 1
)

)
Jul 17 '05 #1
15 5155
lawrence wrote:
$simple = <<<END
<item>
<cbId>345</cbId>
<cbHeadline>Hey there</cbHeadline>
<cbMainContent>God is not dead, he is merely
sleeping.</cbMainContent>
</item>
<item>
<cbId>432</cbId>
<cbHeadline>What a dog, what a dog</cbHeadline>
<cbMainContent>In the manger.</cbMainContent>
</item>
END;


This is not well-formed XML. You need to enclose the item tags inside a root
element; you can only have 1 root element:

<itemlist>
<item>blahblah</item>
<item>blah2blah2</item>
</itemlist>

If you are looking for a simple way to handle XML content without having to
use a DOM parser, have a look at ActiveLink PHP XML Package:

http://www.active-link.com/software/
http://php-xml.sourceforge.net/

Comes with its own parser; read/modify/delete/update XML content with an
easy to use interface; purely written in PHP, does NOT depend on or use any
other XML extension/module. Disclaimer: I am the author of the package.
Jul 17 '05 #2
Zurab Davitiani <ag*@mindless.com> wrote in message news:<xQ*******************@newssvr29.news.prodigy .com>...
This is not well-formed XML. You need to enclose the item tags inside a root
element; you can only have 1 root element:

<itemlist>
<item>blahblah</item>
<item>blah2blah2</item>
</itemlist>


Thanks much. I did as you suggested and wrapped everything above in a
<channel> </channel> tag. Now what I get is workable, though I there
is much about it that I still don't understand. Why does the CHANNEL
array have 4 mentions of CHANNEL, when it only shows up twice, the
opening tag <channgel> and the closing tag </channel>? Why does the
ITEM array have 14 entries, when there are only 4 ITEM tags, two
opening and two closing? Why after cbId is there another ITEM array,
when there is no such tag there, and why is it given the type 'cdata'?

This is the current output:

Index array
Array
(
[CHANNEL] => Array
(
[0] => 0
[1] => 11
[2] => 22
[3] => 23
)

[ITEM] => Array
(
[0] => 1
[1] => 3
[2] => 4
[3] => 6
[4] => 7
[5] => 9
[6] => 10
[7] => 12
[8] => 14
[9] => 15
[10] => 17
[11] => 18
[12] => 20
[13] => 21
)

[CBID] => Array
(
[0] => 2
[1] => 13
)

[CBHEADLINE] => Array
(
[0] => 5
[1] => 16
)

[CBMAINCONTENT] => Array
(
[0] => 8
[1] => 19
)

)

Vals array
Array
(
[0] => Array
(
[tag] => CHANNEL
[type] => open
[level] => 1
[value] =>

)

[1] => Array
(
[tag] => ITEM
[type] => open
[level] => 2
[value] =>

)

[2] => Array
(
[tag] => CBID
[type] => complete
[level] => 3
[value] => 345
)

[3] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 2
)

[4] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 2
)

[5] => Array
(
[tag] => CBHEADLINE
[type] => complete
[level] => 3
[value] => Hey there, handsome
)

[6] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 2
)

[7] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 2
)

[8] => Array
(
[tag] => CBMAINCONTENT
[type] => complete
[level] => 3
[value] => God is not dead, he is merely sleeping.
)

[9] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 2
)

[10] => Array
(
[tag] => ITEM
[type] => close
[level] => 2
)

[11] => Array
(
[tag] => CHANNEL
[value] =>

[type] => cdata
[level] => 1
)

[12] => Array
(
[tag] => ITEM
[type] => open
[level] => 2
[value] =>

)

[13] => Array
(
[tag] => CBID
[type] => complete
[level] => 3
[value] => 432
)

[14] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 2
)

[15] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 2
)

[16] => Array
(
[tag] => CBHEADLINE
[type] => complete
[level] => 3
[value] => What a dog, yes, what a dog
)

[17] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 2
)

[18] => Array
(
[tag] => ITEM
[value] =>
[type] => cdata
[level] => 2
)

[19] => Array
(
[tag] => CBMAINCONTENT
[type] => complete
[level] => 3
[value] => In the manger, was the cow.
)

[20] => Array
(
[tag] => ITEM
[value] =>

[type] => cdata
[level] => 2
)

[21] => Array
(
[tag] => ITEM
[type] => close
[level] => 2
)

[22] => Array
(
[tag] => CHANNEL
[value] =>

[type] => cdata
[level] => 1
)

[23] => Array
(
[tag] => CHANNEL
[type] => close
[level] => 1
)

)
Jul 17 '05 #3
lawrence wrote:
Thanks much. I did as you suggested and wrapped everything above in a
<channel> </channel> tag. Now what I get is workable, though I there
is much about it that I still don't understand. Why does the CHANNEL
array have 4 mentions of CHANNEL, when it only shows up twice, the
opening tag <channgel> and the closing tag </channel>? Why does the
ITEM array have 14 entries, when there are only 4 ITEM tags, two
opening and two closing? Why after cbId is there another ITEM array,
when there is no such tag there, and why is it given the type 'cdata'?


Because this is the way SAX parsers work - they don't parse the document in
its entirety into a single structure (like DOM), they give you bits and
pieces of information and it's up to you to determine how to use them.
Those "type" values are for you to know whether the tag was opened, its
contents parsed, closed, etc. The xml_parse_into_struct tries to give SAX
events a structural image, but I personally don't think it's very efficient
or user-friendly.

If you don't like SAX, or it's not useful for whatever you are doing -
either use a DOM parser or follow the link to the XML package I gave you in
my previous reply.
Jul 17 '05 #4
Zurab Davitiani <ag*@mindless.com> wrote in message news:<avBNc.3314
Because this is the way SAX parsers work - they don't parse the document in
its entirety into a single structure (like DOM), they give you bits and
pieces of information and it's up to you to determine how to use them.
Those "type" values are for you to know whether the tag was opened, its
contents parsed, closed, etc. The xml_parse_into_struct tries to give SAX
events a structural image, but I personally don't think it's very efficient
or user-friendly.

If you don't like SAX, or it's not useful for whatever you are doing -
either use a DOM parser or follow the link to the XML package I gave you in
my previous reply.


That's interesting about SAX parsers. They don't sound very useful.

I'll look into your package, but let me ask a question about it. What
I really want is simply to grab this XML and get back a 2 dimensional
array, then loop through and get out each ITEM, then loop through each
ITEM and the value of each element. Does your package make it easy for
me to grab a 2 dimensional array out of the above 2 dimensional XML?
If not, should I use a DOM parser for that?
Jul 17 '05 #5
Zurab Davitiani <ag*@mindless.com> wrote in message news:<avBNc.3314
Because this is the way SAX parsers work - they don't parse the document in
its entirety into a single structure (like DOM), they give you bits and
pieces of information and it's up to you to determine how to use them.
Those "type" values are for you to know whether the tag was opened, its
contents parsed, closed, etc. The xml_parse_into_struct tries to give SAX
events a structural image, but I personally don't think it's very efficient
or user-friendly.

If you don't like SAX, or it's not useful for whatever you are doing -
either use a DOM parser or follow the link to the XML package I gave you in
my previous reply.


Okay, I went and read the online tutorials for the ActiveLink PHP XML
Package. The organization and cleaness of the documentation is very
impressive. However, my first reaction is that it does way too much
what I need, and would be way too hard to integrate into my existing
code. I've already a lot of code that writes XML, so including this
package would give me a lot of redundant code, I think.

I admire the clean and intelligent simplicity of the ActiveLink site.
Jul 17 '05 #6
lawrence wrote:
That's interesting about SAX parsers. They don't sound very useful.
SAX parsers are good for parsing large XML documents for specific
information. They are quicker and consumer less memory than DOM; but again,
they are not as useful when you want a tree-like structure.
I'll look into your package, but let me ask a question about it. What
I really want is simply to grab this XML and get back a 2 dimensional
array, then loop through and get out each ITEM, then loop through each
ITEM and the value of each element. Does your package make it easy for
me to grab a 2 dimensional array out of the above 2 dimensional XML?
If not, should I use a DOM parser for that?


If you want to create a 2-dimensional array you could, but you would not
need an array at all. If I took your example, it would be:

$myXML = new XML($simple);
$itemsXMLArray = $myXML->getBranches("channel", "item");
foreach($itemsXMLArray as $itemXML) {
print("cbId: " . $itemXML->getTagContent("item/cbId");
//print other fields here
}

Add some error checking and it's good to go.
Jul 17 '05 #7
Zurab Davitiani wrote:
$myXML = new XML($simple);
$itemsXMLArray = $myXML->getBranches("channel", "item");
foreach($itemsXMLArray as $itemXML) {
print("cbId: " . $itemXML->getTagContent("item/cbId");
//print other fields here
}

Add some error checking and it's good to go.


Damn, missing closing parethesis at the end of print line!
Jul 17 '05 #8
Zurab Davitiani <ag*@mindless.com> wrote in message news:<Jc*****************@newssvr21.news.prodigy.c om>...
lawrence wrote:
That's interesting about SAX parsers. They don't sound very useful.


SAX parsers are good for parsing large XML documents for specific
information. They are quicker and consumer less memory than DOM; but again,
they are not as useful when you want a tree-like structure.
I'll look into your package, but let me ask a question about it. What
I really want is simply to grab this XML and get back a 2 dimensional
array, then loop through and get out each ITEM, then loop through each
ITEM and the value of each element. Does your package make it easy for
me to grab a 2 dimensional array out of the above 2 dimensional XML?
If not, should I use a DOM parser for that?


If you want to create a 2-dimensional array you could, but you would not
need an array at all. If I took your example, it would be:

$myXML = new XML($simple);
$itemsXMLArray = $myXML->getBranches("channel", "item");
foreach($itemsXMLArray as $itemXML) {
print("cbId: " . $itemXML->getTagContent("item/cbId");
//print other fields here
}

Add some error checking and it's good to go.


That's very impressive. What is the license on this software? I see on
the website it is described as "free". Does that mean GPL, public
domain, or rights reserved but the cost is $0?
Jul 17 '05 #9
lawrence wrote:
That's very impressive. What is the license on this software? I see on
the website it is described as "free". Does that mean GPL, public
domain, or rights reserved but the cost is $0?


Thanks for the compliment. The license is included with the download archive
- it's LGPL. Remember to read tutorial and documentation (all class methods
are documented), and, if you end up liking it, enjoy!
Jul 17 '05 #10
Could this package be corrupted? I've downloaded the XML package from
this site twice now:

http://www.active-link.com/software/

I keep trying to open it with WinZip, but I always get an error. Is
the double tar normal?

lk******@geocities.com (lawrence) wrote in message news:<da*************************@posting.google.c om>...
Zurab Davitiani <ag*@mindless.com> wrote in message news:<avBNc.3314
Because this is the way SAX parsers work - they don't parse the document in
its entirety into a single structure (like DOM), they give you bits and
pieces of information and it's up to you to determine how to use them.
Those "type" values are for you to know whether the tag was opened, its
contents parsed, closed, etc. The xml_parse_into_struct tries to give SAX
events a structural image, but I personally don't think it's very efficient
or user-friendly.

If you don't like SAX, or it's not useful for whatever you are doing -
either use a DOM parser or follow the link to the XML package I gave you in
my previous reply.


Okay, I went and read the online tutorials for the ActiveLink PHP XML
Package. The organization and cleaness of the documentation is very
impressive. However, my first reaction is that it does way too much
what I need, and would be way too hard to integrate into my existing
code. I've already a lot of code that writes XML, so including this
package would give me a lot of redundant code, I think.

I admire the clean and intelligent simplicity of the ActiveLink site.

Jul 17 '05 #11
.oO(lawrence)
Could this package be corrupted? I've downloaded the XML package from
this site twice now:

http://www.active-link.com/software/
Works here (WinZip 9).
I keep trying to open it with WinZip, but I always get an error. Is
the double tar normal?


It's a gzipped tarball (tar archives, gzip compresses), quite common.
What version of WinZip do you use?

Try this re-packed version (lower compression rate):

<http://www.mfesser.de/temp/ActiveLink-PHP-XML-Package-0.3.4.zip> [58KB]

Micha
Jul 17 '05 #12
Michael Fesser <ne*****@gmx.net> wrote in message
I keep trying to open it with WinZip, but I always get an error. Is
the double tar normal?


It's a gzipped tarball (tar archives, gzip compresses), quite common.
What version of WinZip do you use?

Try this re-packed version (lower compression rate):

<http://www.mfesser.de/temp/ActiveLink-PHP-XML-Package-0.3.4.zip> [58KB]

Micha


Thanks, I've a working version of the package now. I like the fact
that each class is in its own file, that is how my own code works as
well. That makes it somewhat easy to import. It will, I think, only
take a day or two to get the thing working in my own software. They
copyright notice will of course stay where it is in each file. But I
need to have all the files in one directory, so I'm going to have to
completely change the way the files get imported. Thta will take a bit
of reworking. I'm not sure I understand the import() function yet, but
I assume I'll get rid of it, as my own software has its own, very
particular import method.

I may write some additional comments as I study the code. I'll post so
others learning the code can perhaps benefit from whatever I learn.
The code, on brief review, looks like a model of clarity.
Jul 17 '05 #13
From: Zurab Davitiani (ag*@mindless.com)
I'll look into your package, but let me ask a question about it. What
I really want is simply to grab this XML and get back a 2 dimensional
array, then loop through and get out each ITEM, then loop through each
ITEM and the value of each element. Does your package make it easy for
me to grab a 2 dimensional array out of the above 2 dimensional XML?
If not, should I use a DOM parser for that?


If you want to create a 2-dimensional array you could, but you would
not
need an array at all. If I took your example, it would be:

$myXML = new XML($simple);
$itemsXMLArray = $myXML->getBranches("channel", "item");
foreach($itemsXMLArray as $itemXML) {
print("cbId: " . $itemXML->getTagContent("item/cbId");
//print other fields here
}
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>


That is very clean. I'm just getting to use it now. Your package is
one of the few that I did not write that I've been able to use. With
most classes available on the web the programmer makes too many
assumptions and the code doesn't port well.
Jul 17 '05 #14
If I have an RSS newsfeed like this:

<?xml version="1.0" encoding="utf-8"?><!-- generator="whocares" -->
<rss version="0.92">
<channel>
<title>Website Name</title>
<link>http://www.websiteurl.com</link>
<description>Description of website</description>
<lastbuilddate>Sun, 19 Sep 2004 04:34:52 +0000</lastbuilddate>
<docs>http://backend.userland.com/rss092</docs>

<item>
<title>1st Title</title>
<description>1st Description</description>
<link>http://www.websiteurl.com/1stlink/</link>
</item>

<item>
<title>2nd Title</title>
<description>2nd Description</description>
<link>http://www.websiteurl.com/2ndlink/</link>
</item>
</channel>
</rss>

And the following code is used to parse that file/feed:

<?php
$feed = 'http://www.example.com/feed/rss/';

/* ...create and XML parser... */
$xml_parser = xml_parser_create();

/* ...open the feed and parse it... */
$fp = @fopen($feed, 'rb');
if (is_resource($fp)) {
xml_parse_into_struct( $xml_parser, $fp, $vals, $index );
}
@fclose($fp);

/* ...free parser */
xml_parser_free( $xml_parser );
?>

How do I extract the values from $xml_parser?
Jul 17 '05 #15
Argh! I thought I was making a new post. Disregard.

--
--
Remove the period between my first and last name if you wish to e-mail me
directly
"Charles Stricklin" <ch**************@bellsouth.net> wrote in message
news:C8********************@bignews6.bellsouth.net ...
If I have an RSS newsfeed like this:

<?xml version="1.0" encoding="utf-8"?><!-- generator="whocares" -->
<rss version="0.92">
<channel>
<title>Website Name</title>
<link>http://www.websiteurl.com</link>
<description>Description of website</description>
<lastbuilddate>Sun, 19 Sep 2004 04:34:52 +0000</lastbuilddate>
<docs>http://backend.userland.com/rss092</docs>

<item>
<title>1st Title</title>
<description>1st Description</description>
<link>http://www.websiteurl.com/1stlink/</link>
</item>

<item>
<title>2nd Title</title>
<description>2nd Description</description>
<link>http://www.websiteurl.com/2ndlink/</link>
</item>
</channel>
</rss>

And the following code is used to parse that file/feed:

<?php
$feed = 'http://www.example.com/feed/rss/';

/* ...create and XML parser... */
$xml_parser = xml_parser_create();

/* ...open the feed and parse it... */
$fp = @fopen($feed, 'rb');
if (is_resource($fp)) {
xml_parse_into_struct( $xml_parser, $fp, $vals, $index );
}
@fclose($fp);

/* ...free parser */
xml_parser_free( $xml_parser );
?>

How do I extract the values from $xml_parser?

Jul 17 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6631
by: lawrence | last post by:
Well, www.php.net seems utterly crashed. Does anyone know how to get an XML stream and turn it into a 2 dimensional array?
0
1558
by: Tomislav Petrovic | last post by:
I found out that if I have xml which contains elements with CDATA with new lines (\r\n or just \n or just \r) and i do a xml_parse_into_struct on it, all new lines are converted to just \r. Is...
4
1758
by: lawrence | last post by:
I can't quite figure out PHP's XML functions. Given an XML stream like the one below, how do I get a 2D array? <xml> <entry> <weblogHeadline> Saddam found in Iraq </weblogHeadline>
3
1937
by: ron.g.chaplin | last post by:
Ok Perhaps, I am going about this the wrong way, but here is what I have. $string_data = array(); $data_flag = 0; //data we want printed flag ... which is everything that is entered in switch()...
26
7031
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
104
16852
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
1897
by: Martin Pritchard | last post by:
Hi, Sorry for my ignorance, but I'm a bit new to C++. I've been handed over a C++ app written in VS2002 which I have to convert to VS2005. Apparently it's been written in a C style, but cannot...
7
3153
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
5
3773
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.