用于在标签名称中使用冒号解析 XML 的 PHP 库?

2022-01-14 00:00:00 xml namespaces php simplexml

我一直在尝试使用 SimpleXML,但它没有'似乎不喜欢这样的 XML:

I've been trying to use SimpleXML, but it doesn't seem to like XML that looks like this:

<xhtml:div>sample <xhtml:em>italic</xhtml:em> text</xhtml:div>

那么什么库会处理看起来像那样的标签(其中有一个冒号)?

So what library will handle tags that look like that (have a colon in them)?

推荐答案

假设你有一些这样的xml.

Say you have some xml like this.

<xhtml:div>
  <xhtml:em>italic</xhtml:em>
  <date>2010-02-01 06:00</date>
</xhtml:div>

你可以像这样访问'em':$xml->children('xhtml', true)->div->em;

You can access 'em' like this: $xml->children('xhtml', true)->div->em;

然而,如果你想要日期字段,这个:$xml->children('xhtml', true)->div->date;不会工作,因为您被困在 xhtml 命名空间中.

however, if you want the date field, this: $xml->children('xhtml', true)->div->date; wont work, because you are stuck in the xhtml namespace.

您必须再次执行 'children' 才能返回默认命名空间:

you must execute 'children' again to get back to the default namespace:

$xml->children('xhtml', true)->div->children()->date;

相关文章