Read a namespaced attribute from a SimpleXmlElement (imported from XMLReader)

后端 未结 1 1212
清歌不尽
清歌不尽 2021-01-12 15:39

I\'m trying to read a large xml file (about 40 MB), and use this data for update the db of my application.

It seems i\'ve found a good compromise in terms of elapsed

相关标签:
1条回答
  • 2021-01-12 16:18

    Attributes with colons in their name have a namespace.

    The part before the colon is a prefix that is registered to some namespace (usually in the root node). To access the namespaced attributes of a SimpleXmlElement you have to pass the namespace to the attributes() method:

    $attributes = $element->attributes('some-namespace'); // or
    $attributes = $element->attributes('g', TRUE);        // and then
    echo $attributes['name'];
    

    The same applies to element children of a node. Access them via the childrens() method

    $children = $element->children('some-namespace'); // or
    $children = $element->children('g', TRUE);        // and then
    echo $children->elementName;
    

    On a sidenote, if you want to import this data to your database, you can also try to do so directly:

    • http://dev.mysql.com/tech-resources/articles/xml-in-mysql5.1-6.0.html#xml-5.1-importing
    0 讨论(0)
提交回复
热议问题