从 SimpleXmlElement 读取命名空间属性(从 XMLReader 导入)
我正在尝试读取一个大的 xml 文件(大约 40 MB),并使用此数据来更新我的应用程序的数据库。
看来我使用 XMLReader 和 simplexml_import_dom() 在经过的时间/内存方面找到了一个很好的折衷方案,但我无法获取名称中带有冒号的属性的值...例如
。
如果我只是对每个“产品”节点使用 $reader->read() 函数,我可以将值检索为 $reader->value,但是如果我 Expand() 节点并使用 $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');
}
}
}
可能我错过了一些东西......任何建议都会非常感激!
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
名称中带有冒号的属性具有命名空间。
冒号之前的部分是注册到某个名称空间(通常在根节点中)的前缀。要访问
SimpleXmlElement
的命名空间属性,您必须将命名空间传递给attributes()
方法:这同样适用于节点的元素子元素。通过
childrens()
方法访问它们顺便说一句,如果您想将此数据导入数据库,您也可以尝试直接这样做:
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 theattributes()
method:The same applies to element children of a node. Access them via the
childrens()
methodOn a sidenote, if you want to import this data to your database, you can also try to do so directly: