472,809 Members | 4,149 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,809 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 2557
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.