0

I am trying to parse a larger xml file and it has lots of namespaces and is rather complex.

Here is an example of a the entry in the root that I would like to parse: http://pastebin.com/zJGQZBMV

And this is my PHP Script:

private function importFromFile()
{
    $result = false;
    $xmlFile = simplexml_load_file($this->getSavedFile());
    foreach($xmlFile->children() as $entry){
        $entry->registerXPathNamespace("vuln", "http://scap.nist.gov/schema/vulnerability/0.4");
        var_dump($entry->{"vuln:cve-id"}) ;
        break;
    }
    return $result;
}

However, it does not show the CVE Id. Could it be, because of the Namespace ? I receive:

object(SimpleXMLElement)#6 (0) { }

I followed: Parse XML with Namespace using SimpleXML

Kind regards, Richard

Community
  • 1
  • 1
Richard
  • 1,543
  • 3
  • 25
  • 49

1 Answers1

-1

You can parse large xml files with SimpleXML if you allocate enough memory to the script.

Assuming that $this->getSavedFile() represents the complete xml request with root as well as namespace declarations.

$xmlFile = new SimpleXMLElement($this->getSavedFile());

echo $xmlFile->entry['id'];    

The output should be.

CVE-2014-0001

Reference: http://www.php.net/manual/en/simplexmlelement.construct.php