473,387 Members | 1,493 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,387 software developers and data experts.

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 2587
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: marktm | last post by:
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...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
2
by: fizzy | last post by:
i am fetching an xml document with the following structure: <?xml version="1.0" encoding="UTF-8"?> <DTCResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
3
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects...
0
by: Robert Bevington | last post by:
Hi everyone, I've written some code that parses an XML file. The whole thing works correctly, but it's really slow. My example only has two elem_x elements. A normal XML file could theoretically...
8
by: pradeepsarathy | last post by:
Hi all, Does the SAX parser has eventhandlers for parsing xml schema. Can we parse the xml schema the same way as we parse the xml document using SAX Parser. Thanks in advance. -pradeep
1
by: Martin Pöpping | last post by:
Hello, I´ve a problem with parsing a double value from an xml file. My code looks like this: int concept_id; double rank; XmlElement root = documentXMLString.DocumentElement; XmlNodeList...
0
by: Divya Prakash | last post by:
Hi But I am unable to parse all the nodes of the tree .....especially the subtree of the main tree It displays only the sibling not the subtree Regards Divya
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.