Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 time/memory using XMLReader and simplexml_import_dom() but i can't get the value of attributes with colon in their name... for example <g:attr_name>.

If i simply use $reader->read() function for each "product" node i can retrive the value as $reader->value, but if i expand() the node and copy it with $doc->importNode this attributes are ignored.

    $reader = new XMLReader();
    $reader->open(__XML_FILE__);
    $doc = new DOMDocument;

    while ($reader->read()) {
        switch ($reader->nodeType) {
            case (XMLREADER::ELEMENT):
                if($reader->localName=="product"){
                   $node = simplexml_import_dom($doc->importNode($reader->expand(), true));
                   echo $node->attr_name."<br><br>";
                   $reader->next('product');

                } 

        }
    }

Probably i miss something... any advice would be really appriciated!

Thanks.

like image 237
Luciano Avatar asked May 14 '11 12:05

Luciano


1 Answers

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
like image 199
Gordon Avatar answered Oct 22 '22 18:10

Gordon