By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,731 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,731 IT Pros & Developers. It's quick & easy.

php and xml question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.