SimpleXML get node value

后端 未结 3 615
清歌不尽
清歌不尽 2020-12-11 02:11

Say I have this following XML structure:


some value<
相关标签:
3条回答
  • 2020-12-11 02:51

    Variant for xpath (Also how to get content of node having dashes in name):

    <?xml version="1.0" encoding="UTF-8"?> <main>
    <parent>
        <child-1>some value</child-1>
        <child-2>another value</child-2>
    </parent> </main>
    
    $xml = simplexml_load_string($content);
    $node_value= (string)$xml->xpath('parent/child-1')[0];
    

    result of $node_value:

    "some value"

    0 讨论(0)
  • 2020-12-11 03:00
    $xml = new SimpleXMLElement($xml);
    $this->xmlcode = (string) $xml->parent[0]->child1;
    
    0 讨论(0)
  • 2020-12-11 03:07

    A good example of using XPath with php for the SimpleXMLElement can be found here http://www.php.net/manual/en/class.simplexmlelement.php#95229

    // Find the topmost element of the domDocument
    $xpath = new DOMXPath($xml);
    $child1 = $xpath->evaluate('/main/parent/child1')->item(0); 
    
    0 讨论(0)
提交回复
热议问题