Read the namespaced attribute from the SimpleXmlElement element (imported from XMLReader)

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

I seem to have found a good compromise in terms of elapsed time / memory using XMLReader and simplexml_import_dom (), but I cannot get the value of the attributes with a colon in their name ... for example <g:attr_name> .

If I just use the $ reader-> read () function for each "product" node, I can extract the value as the value of $ reader->, but if I expand () the node and copy it using $ doc-> importNode, these 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'); } } } 

I probably missed something ... any advice would really be appreciated!

Thanks.

+6
source share
1 answer

Attributes with colons in their name have a namespace .

The part before the colon is the prefix that is registered in some namespace (usually in the root directory of the node). To access attributes with SimpleXmlElement names, you need 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 goes for node children. Accessing them using the childrens() method

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

In the browser, if you want to import this data into your database, you can also try to do this directly:

+6
source

Source: https://habr.com/ru/post/888104/


All Articles