Log in

View Full Version : Changing DOM code so it works with PHP5



CiaNZ
04-01-2009, 04:16 AM
Hi, can anyone show me the best way to make this code compatible with DOM in PHP5. The error seems to stem from domxml_xmltree. It worked with PHP4, and i've been trying (with no success) to get it working with PHP5. I even tried a compatiblity wrapper which did nothing. Cheers :)



<?
$file = $_SERVER["DOCUMENT_ROOT"]."/include/regex.xml";
if (!($fp = fopen($file, "r")))
{
die("could not open XML input");
}
$data = fread($fp, filesize($file));
fclose($fp);

$aXMLParams = array();

$oTmp = domxml_xmltree($data);

$aSections = $oTmp->children[0]->children;


foreach ($aSections as $k=>$v)
{
if($v->type == 1)
{
$aXMLParams[$v->attributes[0]->value] = array();
$aItems = $v->children;
foreach ($aItems as $kk=>$vv)
{
if($vv->type == 1)
{
$aItemProps = $vv->children;
foreach ($aItemProps as $kkk=>$vvv)
{
if($vvv->type == 1)
{
if ($vvv->tagname == "name")
{
$name = $vvv->children[0]->content;
}
else
{
$aNewItemProps[$vvv->tagname] = $vvv->children[0]->content;
}
}
}
$aXMLParams[$v->attributes[0]->value][$name] = $aNewItemProps;
}
}
}
}
?>

JasonDFR
04-01-2009, 05:51 AM
You've got two options.

Convert to DOM http://www.php.net/manual/en/book.dom.php

Convert to SimpleXML http://www.php.net/manual/en/book.simplexml.php

domxml_xmltree does not exist in PHP5.

I'll try to work out some code examples base on your code. Hopefully that will get you started.

J

JasonDFR
04-01-2009, 06:02 AM
Post an example of regex.xml if you can.

It is hard for me to understand what is going on in your code after you've got the $aSections variable. Hopefully the code below will at least get you started.



$file = $_SERVER["DOCUMENT_ROOT"]."/include/regex.xml";

$xml = file_exists($file) ? simplexml_load_file($file) : false;

if ( $xml ) {

$aSections = $xml->children();

} else {
// NO XML
}

CiaNZ
04-01-2009, 07:37 AM
Thanks for the help Jason, i'll give that a shot as soon as I get home. I'm a bit of a PHP newbie and had this system programmed for me years ago. It's been sitting around ever since. Here's a snippet of the regex file:



<section name="FetchUserInfo">
<item>
<name>
<![CDATA[UpdateId&Nick]]>
</name>
<comment>
<![CDATA[UpdateId&Nick

Update TradeMe id & nick
match groups:
1 - user ID
2 - user nickname]]>
</comment>
<url>
<![CDATA[http://www.trademe.co.nz/MyTradeMe/Default.aspx]]>
</url>
<regex>
<![CDATA[/Listings\.aspx\?member=(\d+).*?<b>([^<>]+)<\/b>/i]]>
</regex>
</item>
<item>
<name>
<![CDATA[GetFavCategoriesList]]>
</name>
<comment>
<![CDATA[GetFavCategoriesList

Get user's favourite categories list
match groups:
1 - category ID]]>
</comment>
<url>
<![CDATA[http://www.trademe.co.nz/MyTradeMe/Favourites.aspx?pv=2]]>
</url>
<regex>
<![CDATA[/href="\/Browse\/CategoryListings.aspx\?mcat=([\d-]+)&amp;tempregid=\d+"/i]]>
</regex>
</item>
<item>
<name>
<![CDATA[GetFavUsersList]]>
</name>
<comment>
<![CDATA[GetFavUsersList

Get user's favourite users list
match groups:
1 - favorite user ID]]>
</comment>
<url>
<![CDATA[http://www.trademe.co.nz/MyTradeMe/Favourites.aspx?pv=3]]>
</url>
<regex>
<![CDATA[/href="\/Members\/Listings.aspx\?member=([\d]+)"/i]]>
</regex>
</item>
</section>