473,395 Members | 1,454 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,395 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 1729
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.