Luc*_*ano 6 php xmlreader simplexml domdocument
我正在尝试读取一个大的xml文件(大约40 MB),并使用此数据更新我的应用程序的数据库.
看来我使用XMLReader和simplexml_import_dom()在经过的时间/内存方面找到了一个很好的折衷方案但是我无法在名称中获得带冒号的属性值...例如<g:attr_name>.
如果我只是为每个"产品"节点使用$ reader-> read()函数,我可以将值作为$ reader-> value进行检索,但如果我展开()节点并使用$ doc-> importNode复制它,则此属性为忽略.
$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');
}
}
}
Run Code Online (Sandbox Code Playgroud)
可能我会想念一些事情......任何建议都会非常贴切!
谢谢.
名称中带冒号的属性具有名称空间.
冒号前面的部分是一个注册到某个命名空间的前缀(通常在根节点中).要访问a的命名空间属性,SimpleXmlElement必须将命名空间传递给attributes()方法:
$attributes = $element->attributes('some-namespace'); // or
$attributes = $element->attributes('g', TRUE); // and then
echo $attributes['name'];
Run Code Online (Sandbox Code Playgroud)
这同样适用于节点的元素子节点.通过该childrens()方法访问它们
$children = $element->children('some-namespace'); // or
$children = $element->children('g', TRUE); // and then
echo $children->elementName;
Run Code Online (Sandbox Code Playgroud)
在旁注中,如果要将此数据导入数据库,还可以尝试直接执行此操作:
| 归档时间: |
|
| 查看次数: |
2476 次 |
| 最近记录: |