468,771 Members | 1,513 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Need help parsing XML attributes

Hi-

I am using the following code and want to retrieve an attributes value.
I want to loop through the xml file and add the attribute values to an
array. The line in question is marked with "<---??????" below.

I have included both the PHP and XML.

Your help will be much appreciated.

Thanks.

PHP Source
################################################## #############
<?php
$headlines = array();
$urls = array();
$match = "";

function opening_element($parser, $element, $attributes) {
global $match;

if ($element == "HeadLine"){
$match = "headline";
}
elseif ($element == "NewsItemRef"){
$match = "url";
}
}
function closing_element($parser, $element) {

}
function character_data($parser, $data) {
global $match;
if ($match == "headline") {
global $headlines;
$headlines[] = $data;
}
elseif($match == "url") {
global $urls;
$urls[] = $data[1]; //<---??????
}
$match = "";
}

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, true);
xml_set_element_handler($parser, "opening_element", "closing_element");
xml_set_character_data_handler($parser, "character_data");
$document = file("index.xml");
foreach ($document as $line) {
xml_parse($parser, $line);
}

foreach ($headlines as $name) {
echo "$name<br>\n";
}
foreach ($urls as $url) {
echo "$url<br>";
}
xml_parser_free($parser);

?>
XML Source
################################################## #############
<?xml version="1.0" encoding="iso-8859-1"?>
<NewsML>
<Catalog Href="http://www.afp.com/dtd/AFPCatalog.xml"/>
<NewsEnvelope>
<DateAndTime>20050409T055627Z</DateAndTime>
</NewsEnvelope>
<NewsItem>
<Identification>
<NewsIdentifier>
<ProviderId>afp.com</ProviderId>
<DateId>20050409</DateId>
<NewsItemId>ext--english--airlineweekly--airlines</NewsItemId>
<RevisionId PreviousRevision="0" Update="N">1</RevisionId>

<PublicIdentifier>urn:newsml:afp.com:20050409:ex t--english--airlineweekly--airlines:1</PublicIdentifier>
</NewsIdentifier>
<NameLabel></NameLabel>
</Identification>
<NewsManagement>
<NewsItemType FormalName="News"/>
<FirstCreated>20050409T055627Z</FirstCreated>
<ThisRevisionCreated>20050409T055627Z</ThisRevisionCreated>
<Status FormalName="Usable"/>
</NewsManagement>
<NewsComponent>
<AdministrativeMetadata>
<Provider>
<Party FormalName="AFP"/>
</Provider>
</AdministrativeMetadata>
<DescriptiveMetadata>
<Language FormalName="en"/>
</DescriptiveMetadata>
<NewsComponent>
<NewsLines>
<HeadLine>US aviation security chief resigns following
criticism</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050409055415.7x7vzi3y.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>US regrets no deal with EU on Airbus-Boeing
handouts</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408221056.h3y3hbvu.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Bush says his Christian faith reaffirmed at
pontiff's service</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408142343.99d8pr2a.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>More than 130 flights canceled in two days for
papal funeral</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408140553.liphj5ts.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Gabon president determined to save troubled Air
Gabon</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408101026.9ew2g9fc.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Korean Air plans regular flights to western China's
Xinjiang</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408082516.r5qvpx32.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Parents of air stowaway sue airport, air company
for negligence</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408062638.2uf11c8s.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Qantas hikes fuel surcharge after oil price
surge</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408061511.uuemx1pw.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Former Garuda Indonesia top officials, pilot
questioned over activist death</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408045039.qv2tzxpc.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Vietnam Airlines plans direct flights to
Germany</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408044048.fg8ehp6v.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Camel costume prank sparks luggage handling probe
at Australia's Qantas</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050408043122.3c48ldbq.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Top EU competition official rails against
protection for big firms</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050407164004.3aikzi89.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Anti-aircraft missiles, marksmen: Rome smothered
under security blanket</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050407145944.2kxez8zg.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Dutch government reduces stake in Air
France-KLM</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050407122530.3e23mlbh.xml"/>
</NewsComponent>
<NewsComponent>
<NewsLines>
<HeadLine>Thai Airways unveils new corporate makeover,
aircraft</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="050407113559.zziklpv1.xml"/>
</NewsComponent>
</NewsComponent>
</NewsItem>
</NewsML>

Jul 17 '05 #1
2 2422
ma****@gmail.com wrote:
function character_data($parser, $data) {
global $match;
if ($match == "headline") {
global $headlines;
$headlines[] = $data;
}
elseif($match == "url") {
global $urls;
$urls[] = $data[1]; //<---??????
}
$match = "";
}


function opening_element($parser, $element, $attributes) {
global $match, $urls;

if ($element == "HeadLine"){
$match = "headline";
} elseif ($element == "NewsItemRef"){
$urls[] = $attributes['NewsItem'];
}
}

function character_data($parser, $data) {
global $match, $headlines;

if ($match == "headline") {
$headlines[] = $data;
}
$match = "";
}
JW

Jul 17 '05 #2
Thanks! This is perfect!

Reagrds,
Mark

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by marktm | last post: by
16 posts views Thread by Terry | last post: by
reply views Thread by Robert Bevington | last post: by
8 posts views Thread by pradeepsarathy | last post: by
1 post views Thread by Martin Pöpping | last post: by
reply views Thread by Divya Prakash | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.