473,395 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

XML_PARSE_INTO_STRUCT

Hi,
I am trying to parse an xml file and I am having some problems. Here
is a sample structure of my xml file.

<title>Example</title>
<link rel="self" href="http://link0.com"/>

<entry>
<title>E1</title>
<link href="http://link1.com"/>
</entry>
<entry>
<title>E2</title>
<link href="http://link2.com"/>
</entry>

I want to be able to output the following

<p><a href="http://link0.com'">Example</a></p>
<p><a href="http://link1.com'">E1</a></p>
<p><a href="http://link2.com'">E2</a></p>

here is my code:
$file="test.xml";

$xml_parser = xml_parser_create();

$handle = fopen($file, "rb");
while (!feof($handle)) {
$data .= fread($handle, 8192);
}
fclose($handle);

xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

foreach ($vals as $xml_elem) {
if ($xml_elem['level'] == 1){
// THIS IS WHERE I SOMEHOW GOING TO READ AHEAD OR ACCESS THE NEXT
TAG=LINK AND SOMEHOW READ ITS ATTRIBUTE
if ($xml_elem['tag'] == 'TITLE')
printf ("<h1>%s</h1>\n", $xml_elem['value']);
else if ($xml_elem['tag'] == 'SUBTITLE')
printf ("<h3>%s</h3>\n", $xml_elem['value']);
}

The problem is write here because i don't know how I can first read
the LINK attribute first and then the title.

Thanks

Jun 16 '07 #1
1 2801
On 17 Cze, 01:23, Jack <accpac...@hotmail.comwrote:
Hi,
I am trying to parse an xml file and I am having some problems. Here
is a sample structure of my xml file.

<title>Example</title>
<link rel="self" href="http://link0.com"/>

<entry>
<title>E1</title>
<link href="http://link1.com"/>
</entry>
<entry>
<title>E2</title>
<link href="http://link2.com"/>
</entry>

I want to be able to output the following

<p><a href="http://link0.com'">Example</a></p>
<p><a href="http://link1.com'">E1</a></p>
<p><a href="http://link2.com'">E2</a></p>

here is my code:
$file="test.xml";

$xml_parser = xml_parser_create();

$handle = fopen($file, "rb");
while (!feof($handle)) {
$data .= fread($handle, 8192);}

fclose($handle);

xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

foreach ($vals as $xml_elem) {
if ($xml_elem['level'] == 1){
// THIS IS WHERE I SOMEHOW GOING TO READ AHEAD OR ACCESS THE NEXT
TAG=LINK AND SOMEHOW READ ITS ATTRIBUTE
if ($xml_elem['tag'] == 'TITLE')
printf ("<h1>%s</h1>\n", $xml_elem['value']);
else if ($xml_elem['tag'] == 'SUBTITLE')
printf ("<h3>%s</h3>\n", $xml_elem['value']);
}

The problem is write here because i don't know how I can first read
the LINK attribute first and then the title.

Thanks
I think that SimpleXML would be more convenient for your task. Below
you have sample code:

<?php
$xml = "<test>".
"<entry><title>Test1</title><link>www.test1.org</link></entry>".
"<entry><title>Test2</title><link>www.test2.org</link></entry>".
"</test>";

$xmlTable = simplexml_load_string($xml); //Use
simplexml_load_file($filename) for file loading

for ($i = 0; $i < count($xmlTable->entry); $i++){
echo '<p><a href="'.$xmlTable->entry[$i]->link.'">'.$xmlTable-
>entry[$i]->title.'</a></p>';
}
?>
Jun 17 '07 #2

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

Similar topics

0
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...
0
by: Phil Powell | last post by:
// PROCESS XML CONTENT INTO DYNAMICALLY-NAMED ARRAYS foreach (array('mime', 'state', 'country') as $val) { $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);...
0
by: lawrence | last post by:
I did a little test to learn about how xml_parse_into_struct() works and I find, to my surprise, that it fails to work at all with two different versions of RSS. But it works with an Atom feed just...
15
by: lawrence | last post by:
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>
3
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()...
4
by: Oliver Spiesshofer | last post by:
Hi, is there a script out there that can send RSS feeds to a given email account? I have search the usual suspects (google, SF, FM, hotscripts) but could not find anything thanks Oliver
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
12
abdoelmasry
by: abdoelmasry | last post by:
HI men im trying To get xml file conetent To insert to database xml parser functions couldn't get single element from xml file it's return all start elements , end elements and data elements...
3
by: sisqorap | last post by:
Hi all, I have this kind of warning and would like to have it solved: Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
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,...

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.