SimpleXML and namespaces

后端 未结 3 1313
无人及你
无人及你 2020-12-07 03:02

I have following code.

 
  
   24
   6
  

        
相关标签:
3条回答
  • 2020-12-07 03:39

    Try this:

     <?php 
        $entry = simplexml_load_file('entry.xml');        
        printf("%s\n", $entry->children('job', true)->location->region);  
     ?> 
    

    To check the above code in action, click here

    For more information about SimpleXml refer to this article

    0 讨论(0)
  • 2020-12-07 03:47

    I would do it dynamically.

    $xml = @simplexml_load_string($path) // loads your valid xml data
    foreach($xml->channel->item as $entry) {
    
      $namespaces = $entry->getNameSpaces(true);
      foreach($namespaces as $ns=>$value)
      {
        $job = $entry->children($namespaces[$ns]);
        $author = (string)$job->creator;
    
        if ($author != "")
        {
          $someVariable = (string) $dc->creator;
        }
    }
    
    0 讨论(0)
  • 2020-12-07 04:02

    You should register the job namespace, then you can use the registered namespace-prefix in an XPath to select what you want:

    $sxe = new SimpleXMLElement($xml);
    
    $sxe->registerXPathNamespace('job', 'http://example.org/you-did-not-provide-the-job-namespaceURI-in-your-example');
    $result = $sxe->xpath('//entry/job:location/job:region');
    
    foreach ($result as $location) {
      echo $location . "\n";
    }
    
    0 讨论(0)
提交回复
热议问题