[PHP DOMXML] Content :?

Pagina: 1
Acties:

  • FlipFlap
  • Registratie: Februari 2000
  • Laatst online: 07-08 22:09

FlipFlap

Tweakeroid sinds 28_01_2001

Topicstarter
Ik heb een XML parser gemaakt voor een t.net tracker en hoera, hoera, hoera het werkt. Zie www.flipflap.nl/tweakers.php

Vervolgens installeer ik mijn 2e server opnieuw en plaats daar precies hetzelfde script op www.dubbelf.com/tweakers.php en........ geen data.

De nieuwe installatie draait met php 4.2.1 (4.0.6) met libxml 2.4.1 (2.2.11).

De source:
PHP:
1
<?$file = "http://www.tweakers.net/turbotracker.dsp";$dom = xmldocfile($file);$root = $dom->root();$nodes = $root->children();$counter = 0;for ($x=0; $x<sizeof($nodes); $x++){        if ($nodes[$x]->type == XML_ELEMENT_NODE)        {            $thisNode = $nodes[$x];            $childNodes = $thisNode->children();            for($y=0; $y<sizeof($childNodes); $y++)            {                if ($childNodes[$y]->type == XML_ELEMENT_NODE)                {                    if ($childNodes[$y]->name == "id")          {$counter++; $id[$counter] = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "titel")        {$titel[$counter]         = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "editor")        {$editor[$counter]         = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "categorie")    {$cat[$counter]         = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "bron")          {$bron[$counter]         = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "link")        {$link[$counter]         = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "tijd")         {$tijd[$counter]         = $childNodes[$y]->content; }                    if ($childNodes[$y]->name == "reacties")     {$reacties[$counter]    = $childNodes[$y]->content; }                }            }        }}?>

Uiteindelijk na wat testen blijkt dat de volgende zaken niet meer werken:
$childNodes[$y]->name
$childNodes[$y]->content

Die geven beide geen waarden meer. Ik heb bij php.net lopen zoeken, maar kan daar geen oplossing vinden. Iemand?

ff niets


  • TheMrH
  • Registratie: April 2000
  • Laatst online: 01-09 22:45
Yep, ik ken het probleem. Vanaf 4.1 gebruiken ze tagname ipv name, en de content zit in de eerste child van de current node. Staat ook in de user comments beschreven, onderin.
bjlan@hotmail.com
22-Dec-2001 02:46

It seems like DomNode class attribute 'name' has changed to 'tagname' for
nodes of type XML_TEXT_NODE.

The documentation does not mention anything about the changes to domxml
(seems to be more than this one).
Hopefully this will help anyone who had as much trouble as I have using
domxml across different PHP versions...

I am using 4.1.1 now, with libxml2-2.4.13.

It seems that the way the XML tree is represented has changed from
4.06/libxml2-2.3.14 to use multple node types.

I had a lot of trouble figuring out how to get to the TAGNAME and CONTENT
data for my XML tree... but I figured it out and wanted to share:

function getNodeContent($name) {
$child_a = domxml_children(domxml_root($doc));
foreach ($child_a as $node) {
if ($node->tagname() == $name) {
// the CONTENT of a node is in the content field of a
// CHILD node of the tag name.
// <NODE>DATA</NODE> is actually a NODE element
// object with a TEXT element object as its only child.
// THe child TEXT element has the content field with
// the actual content.
// it helps a LOT to do a print_r(xmltree($xml)) and
// look at this as HTML SOURCE because it's nicely indented
$content_a = domxml_children($node);
return $content_a[0]->content;
}
}
}
Ik heb het zelf opgelost door een simpel wrapper classje te schrijven die het werk voor je opknapt tussen de php versies door. Als je interesse hebt let me know...

The box said 'requires Windows 95 or better', so I installed Linux...


  • FlipFlap
  • Registratie: Februari 2000
  • Laatst online: 07-08 22:09

FlipFlap

Tweakeroid sinds 28_01_2001

Topicstarter
Op woensdag 27 maart 2002 22:38 schreef TheMrH het volgende:
Yep, ik ken het probleem. Vanaf 4.1 gebruiken ze tagname ipv name, en de content zit in de eerste child van de current node. Staat ook in de user comments beschreven, onderin.
[..]


[..]

Ik heb het zelf opgelost door een simpel wrapper classje te schrijven die het werk voor je opknapt tussen de php versies door. Als je interesse hebt let me know...
De tagname werkt idd, maar hoe dat nu zit met de content snap ik niet echt.

Overigens ben ik iig geïnteresseerd in je wrapper classje. Het is voor mij een klein projectje om ook eens aan XML te hebben geroken, maar ik ben benieuwd hoe je het zelf opgelosd hebt.

ff niets


  • TheMrH
  • Registratie: April 2000
  • Laatst online: 01-09 22:45
Hier istie dan:
PHP:
1
<?class XMLNode{    function XMLNode(&amp;$node)    {        $this->_node = $node;    }    function &amp;getContent()    {        if (PHP_VERSION >= '4.1.1')        {            $nodechildren = $this->_node->children();            return $nodechildren[0]->content;        }        else        {            return $this->_node->content;        }    }    function &amp;getTagName()    {        if (PHP_VERSION >= '4.1.1')        {            return $this->_node->tagname;        }        else        {            return $this->_node->name;        }    }    function getType()    {        return $this->_node->type;    }}?>

En dit is een snippet om hem te usen:
PHP:
1
<?foreach ($xpathobj->nodeset as $nodeobj){    $nodes =&amp; $nodeobj->children();    foreach ($nodes as $node)    {        $n = new XMLNode($node);        echo $n->getTagname();        if($n->getType() == XML_ELEMENT_NODE)        {            echo 'XML_ELEMENT_NODE has the content '.$tn->getContent();        }    }}?>

The box said 'requires Windows 95 or better', so I installed Linux...


  • FlipFlap
  • Registratie: Februari 2000
  • Laatst online: 07-08 22:09

FlipFlap

Tweakeroid sinds 28_01_2001

Topicstarter
TheMrH:

Een beetje late reactie ivm drukte enzo, maar: Het werkt!!! :* :)

ff niets


  • TheMrH
  • Registratie: April 2000
  • Laatst online: 01-09 22:45
Mooi zo!

The box said 'requires Windows 95 or better', so I installed Linux...

Pagina: 1