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 == "NewsItemRe f"){
$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_crea te();
xml_parser_set_ option($parser, XML_OPTION_CASE _FOLDING, false);
xml_parser_set_ option($parser, XML_OPTION_SKIP _WHITE, true);
xml_set_element _handler($parse r, "opening_elemen t", "closing_elemen t");
xml_set_charact er_data_handler ($parser, "character_data ");
$document = file("index.xml ");
foreach ($document as $line) {
xml_parse($pars er, $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>20 050409T055627Z</DateAndTime>
</NewsEnvelope>
<NewsItem>
<Identification >
<NewsIdentifier >
<ProviderId>afp .com</ProviderId>
<DateId>2005040 9</DateId>
<NewsItemId>e xt--english--airlineweekly--airlines</NewsItemId>
<RevisionId PreviousRevisio n="0" Update="N">1</RevisionId>
<PublicIdentifi er>urn:newsml:a fp.com:20050409 :ext--english--airlineweekly--airlines:1</PublicIdentifie r>
</NewsIdentifier>
<NameLabel></NameLabel>
</Identification>
<NewsManagement >
<NewsItemType FormalName="New s"/>
<FirstCreated>2 0050409T055627Z </FirstCreated>
<ThisRevisionCr eated>20050409T 055627Z</ThisRevisionCre ated>
<Status FormalName="Usa ble"/>
</NewsManagement>
<NewsComponen t>
<Administrative Metadata>
<Provider>
<Party FormalName="AFP "/>
</Provider>
</AdministrativeM etadata>
<DescriptiveMet adata>
<Language FormalName="en"/>
</DescriptiveMeta data>
<NewsComponen t>
<NewsLines>
<HeadLine>US aviation security chief resigns following
criticism</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 9055415.7x7vzi3 y.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>US regrets no deal with EU on Airbus-Boeing
handouts</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8221056.h3y3hbv u.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Bus h says his Christian faith reaffirmed at
pontiff's service</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8142343.99d8pr2 a.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Mor e than 130 flights canceled in two days for
papal funeral</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8140553.liphj5t s.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Gab on president determined to save troubled Air
Gabon</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8101026.9ew2g9f c.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Korea n Air plans regular flights to western China's
Xinjiang</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8082516.r5qvpx3 2.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Paren ts of air stowaway sue airport, air company
for negligence</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8062638.2uf11c8 s.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Qanta s hikes fuel surcharge after oil price
surge</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8061511.uuemx1p w.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Forme r Garuda Indonesia top officials, pilot
questioned over activist death</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8045039.qv2tzxp c.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Vietn am Airlines plans direct flights to
Germany</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8044048.fg8ehp6 v.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Cam el costume prank sparks luggage handling probe
at Australia's Qantas</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8043122.3c48ldb q.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Top EU competition official rails against
protection for big firms</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7164004.3aikzi8 9.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Ant i-aircraft missiles, marksmen: Rome smothered
under security blanket</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7145944.2kxez8z g.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Dut ch government reduces stake in Air
France-KLM</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7122530.3e23mlb h.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Tha i Airways unveils new corporate makeover,
aircraft</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7113559.zziklpv 1.xml"/>
</NewsComponent>
</NewsComponent>
</NewsItem>
</NewsML>
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 == "NewsItemRe f"){
$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_crea te();
xml_parser_set_ option($parser, XML_OPTION_CASE _FOLDING, false);
xml_parser_set_ option($parser, XML_OPTION_SKIP _WHITE, true);
xml_set_element _handler($parse r, "opening_elemen t", "closing_elemen t");
xml_set_charact er_data_handler ($parser, "character_data ");
$document = file("index.xml ");
foreach ($document as $line) {
xml_parse($pars er, $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>20 050409T055627Z</DateAndTime>
</NewsEnvelope>
<NewsItem>
<Identification >
<NewsIdentifier >
<ProviderId>afp .com</ProviderId>
<DateId>2005040 9</DateId>
<NewsItemId>e xt--english--airlineweekly--airlines</NewsItemId>
<RevisionId PreviousRevisio n="0" Update="N">1</RevisionId>
<PublicIdentifi er>urn:newsml:a fp.com:20050409 :ext--english--airlineweekly--airlines:1</PublicIdentifie r>
</NewsIdentifier>
<NameLabel></NameLabel>
</Identification>
<NewsManagement >
<NewsItemType FormalName="New s"/>
<FirstCreated>2 0050409T055627Z </FirstCreated>
<ThisRevisionCr eated>20050409T 055627Z</ThisRevisionCre ated>
<Status FormalName="Usa ble"/>
</NewsManagement>
<NewsComponen t>
<Administrative Metadata>
<Provider>
<Party FormalName="AFP "/>
</Provider>
</AdministrativeM etadata>
<DescriptiveMet adata>
<Language FormalName="en"/>
</DescriptiveMeta data>
<NewsComponen t>
<NewsLines>
<HeadLine>US aviation security chief resigns following
criticism</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 9055415.7x7vzi3 y.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>US regrets no deal with EU on Airbus-Boeing
handouts</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8221056.h3y3hbv u.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Bus h says his Christian faith reaffirmed at
pontiff's service</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8142343.99d8pr2 a.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Mor e than 130 flights canceled in two days for
papal funeral</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8140553.liphj5t s.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Gab on president determined to save troubled Air
Gabon</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8101026.9ew2g9f c.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Korea n Air plans regular flights to western China's
Xinjiang</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8082516.r5qvpx3 2.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Paren ts of air stowaway sue airport, air company
for negligence</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8062638.2uf11c8 s.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Qanta s hikes fuel surcharge after oil price
surge</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8061511.uuemx1p w.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Forme r Garuda Indonesia top officials, pilot
questioned over activist death</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8045039.qv2tzxp c.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Vietn am Airlines plans direct flights to
Germany</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8044048.fg8ehp6 v.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Cam el costume prank sparks luggage handling probe
at Australia's Qantas</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 8043122.3c48ldb q.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Top EU competition official rails against
protection for big firms</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7164004.3aikzi8 9.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Ant i-aircraft missiles, marksmen: Rome smothered
under security blanket</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7145944.2kxez8z g.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Dut ch government reduces stake in Air
France-KLM</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7122530.3e23mlb h.xml"/>
</NewsComponent>
<NewsComponen t>
<NewsLines>
<HeadLine>Tha i Airways unveils new corporate makeover,
aircraft</HeadLine>
</NewsLines>
<NewsItemRef NewsItem="05040 7113559.zziklpv 1.xml"/>
</NewsComponent>
</NewsComponent>
</NewsItem>
</NewsML>