1

I have a question regarding reading an xml file with php. I have a sample php file:

///file.xml

 <appender name="app1">
      <param name="param1"></param>
 </appender>

Now i want to get the value of the param name. I have this code.

function getURL($aURL){

$objDOM = new DOMDocument();

////the file.xml    
$objDOM->load($aURL);  
$note = $objDOM->getElementsByTagName("appender");

foreach ( $note as $value)  {
    ///First try
    $name = $value->getElementsByTagName("name")->firstChild->nodeValue;

    ///Second try
    ///$logName = $value->getElementsByTagName("name");
    ///$name = $logName->item(0)->nodeValue;

}

}

For my First and Second try both returns empty. Does not give the "app1" value.

Can someone help me on this?

Thank you.

tinks
  • 323
  • 1
  • 5
  • 21
  • *(related)* [Noob Question about DOMDocument](http://stackoverflow.com/questions/4979836/noob-question-about-domdocument-in-php/4983721#4983721) – Gordon Feb 07 '12 at 09:00
  • possible duplicate of [How to extract a node attribute from XML using PHP's DOM Parser](http://stackoverflow.com/questions/3993302/how-to-extract-a-node-attribute-from-xml-using-phps-dom-parser) – Gordon Feb 07 '12 at 09:00

5 Answers5

2

The name of the tag is param and the name of the attribute is name:

$name = $value->getElementsByTagName("param")->firstChild->getAttribute('name')->nodeValue;
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
1

name is not an element, it's an attribute of the appender element

$name = $value->getElementsByTagName("param")->firstChild->getAttribute("name");

EDIT

$data = '<appender name="app1">
    <param name="param1"></param>
    <param name="param2"></param>
</appender>';

$objDOM = new DOMDocument();
$objDOM->loadXML($data);

$appenders = $objDOM->getElementsByTagName("appender");

foreach ($appenders as $appender)  {
    $params = $appender->getElementsByTagName("param");
    foreach($params as $param) {
        $name = $param->getAttribute("name");
        var_dump($name);
        break;
    }
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • thanks @Mark Baker. but it seems to not work :( gives me this "Object of class DOMElement could not be converted to string in" error – tinks Feb 07 '12 at 10:00
1

@Tinks with "SimpleXML" its a bit easier to read the values.

<?php
  $xmlString = '<appender name="app1">
      <param name="param1" value="2">123</param>
      <param name="param2" value="3">345</param>
 </appender>';

$xml = new SimpleXMLElement($xmlString);

foreach($xml->param as $out) {
    echo $out['name']."  ";
    echo $out['value']."<br />";
}
?>

Here is a short example how to read an xml file.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

Your nodeValue is really empty. According to <param name="param1"></param> , you did not specify one. Use getAttribute method instead.

Raptor
  • 53,206
  • 45
  • 230
  • 366
-1

Here's what to do:

Load the XML file
Get the name of the first element
Create a loop that will trigger on each child node, using the children() function
Output the element name and data for each child node