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

php and xml question

Hi

Im using php to parse an xml file of below format. I have no problem
extracting the various values within the [resource] tags (lib, id, url,
file etc), but cant get hold of whats in the [drt] tag (max_page, page
etc) nor whats whithin the [first_url], [last_url], [next_url] tags ...

Can you suggest a generic parser and how to grab these or any other
tips on what Im missing here :/

Appreciate any pointers!..
Thanks

Example file:

<?xml version="1.0" encoding="ISO-8859-15"?>
<!DOCTYPE drt PUBLIC "-//XML ENGINE//DTD">
<drt max_page="3" page="1" results_displayed="25" total_results="64">
<ressource>
<lib>lib1</lib>
<id>id1</id>
<url_download>url1</url_download>
<file>file1</file>
<copyright>copy1</copyright>
</ressource>
<ressource>
<lib>lib2</lib>
<id>id2</id>
<url_download>url2</url_download>
<file>file2</file>
</ressource>
<first_url>abc</first_url>
<last_url>def</last_url>
<next_url>ghi</next_url>
</drt>

Jul 17 '05 #1
2 1723
as******************************@yahoo.com wrote:
Hi

Im using php to parse an xml file of below format. I have no problem
extracting the various values within the [resource] tags (lib, id, url,
file etc), but cant get hold of whats in the [drt] tag (max_page, page
etc) nor whats whithin the [first_url], [last_url], [next_url] tags ...

Can you suggest a generic parser and how to grab these or any other
tips on what Im missing here :/

Appreciate any pointers!..
Thanks

Example file:

<?xml version="1.0" encoding="ISO-8859-15"?>
<!DOCTYPE drt PUBLIC "-//XML ENGINE//DTD">
<drt max_page="3" page="1" results_displayed="25" total_results="64">

I don't know how you currently parse the XML, but when using the
functions described on http://www.php.net/xml, you could try the following:

function elementHandler($parser, $name, $attr = null) {
if ($name == "drt" && is_array($attr) && count($attr)) {
foreach ($attr as $k => $v) {
print "$k => $v";
}
}
}

xml_set_element_handler($parser, "elementHandler", "elementHandler");

JW

Jul 17 '05 #2
Thanks for your help. This is the full parser code I currently use:

[start parser code]
<?php

/**
* main function for parsing xml file
*
* @param string $file xml file to parse
* @return array result of the parsing
*/

function MPxmlParse($file) {
ini_xml(); //in globals variables
$rXMLPARSER = xml_parser_create();
xml_parser_set_option($rXMLPARSER, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($rXMLPARSER, 'start_element',
'close_element');
xml_set_character_data_handler($rXMLPARSER,'charac ter_data');
$fp = fopen($file, 'r');
while($sXmlStream = fread($fp, 4096)) {
if (!xml_parse($rXMLPARSER, $sXmlStream, feof($fp))) {
die('Error parsing xml file');
}
}
fclose($fp);
$aRet['ressources'] = $GLOBALS[aXmlTotalElements];
$aRet['infos'] = $GLOBALS[xmlInfos];
return $aRet;
}

/**
* start_element
* @private
*/
function start_element($parser, $name, $attrs='') {
$name = strtolower(trim($name));
$GLOBALS[sXmlactualElement] = $name;
if(in_array($name, $GLOBALS[aSpecialElements])) {
$GLOBALS[xmlElementCpt]++;
}elseif($name == 'drt') {
$GLOBALS[aXmlAttrs] = $attrs;
}
} // end func start_element

/**
* close_element
* @private
*/
function close_element($parser, $name, $attrs='') {
$name = strtolower(trim($name));
if($name == 'ressource' && $GLOBALS[sXmlactualElement] != 'file') {
$GLOBALS[xmlElementCpt]++;
}
} // end func close_element

/**
* character_data
* @private
*/
function character_data($parser, $data='') {
if($GLOBALS[sXmlactualElement] != 'ressource'
&& $GLOBALS[sXmlactualElement] != 'drt'
&& !in_array($GLOBALS[sXmlactualElement],
$GLOBALS[aSpecialElements]) ) {

$GLOBALS[aXmlTotalElements][$GLOBALS[xmlElementCpt]][$GLOBALS[sXmlactualElement]].=
trim($data);
}elseif($GLOBALS[sXmlactualElement] == 'drt') {
$GLOBALS[xmlInfos] = $GLOBALS[aXmlAttrs];
}else{
if($GLOBALS[sXmlactualElement] != 'ressource') {
$GLOBALS[xmlInfos][$GLOBALS[sXmlactualElement]].=
trim($data);
}
}
} // end func character_data

/**
* initialize xml variables
*/
function ini_xml() {
$GLOBALS[aXmlTotalElements] = array();
$GLOBALS[sXmlactualElement] = '';
$GLOBALS[xmlElementCpt] = 0;
$GLOBALS[aXmlAttrs] = array();
$GLOBALS[xmlInfos] = array();
$GLOBALS[aSpecialElements] =
array('ressource','first_url','last_url','next_url ','prev_url');
} // end func ini_xml

?>

[end parser code]
I then call this per below:

[start]

$sFile = 'xmlfile';
$aArray = MPxmlParse($sFile);
$aRess = $aArray['ressources'];
$aInfos = $aArray['infos'];
$aDrts = $aArray['drt'];
foreach ($aRess as $aInfo) {
$lib = trim(stripslashes($aInfo['lib']));
if (strlen($lib) > 0){
echo $aInfo['lib'];
echo $aInfo['id'];
}
}

[end]

This way Im able to grab lib and id etc, but not whats within the DRT
or [first_url], [last_url], [next_url] ....

Jul 17 '05 #3

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

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.