我从Google返回了以下DOMNodeList对象,我需要解析它。
我将其解析为DOMElement对象数组,每个警告对应一个:
$new_product = _GSC_AtomParser::parse($resp->body);
$elements = $new_product->getWarnings();
$warnings = array();
foreach ($elements as $element):
$warnings[] = $element;
endforeach;
然后我需要解析这些DOMElement对象以获取警告:
[0] => DOMElement Object
(
[tagName] => sc:warning
[schemaTypeInfo] =>
[nodeName] => sc:warning
[nodeValue] => validation/missing_recommendedShoppinggoogle_product_categoryWe recommend including this attribute.
[nodeType] => 1
[parentNode] => (object value omitted)
[childNodes] => (object value omitted)
[firstChild] => (object value omitted)
[lastChild] => (object value omitted)
[previousSibling] =>
[nextSibling] => (object value omitted)
[attributes] => (object value omitted)
[ownerDocument] => (object value omitted)
[namespaceURI] => http://schemas.google.com/structuredcontent/2009
[prefix] => sc
[localName] => warning
[baseURI] => /home/digit106/dev/public_html/manager/
[textContent] => validation/missing_recommendedShoppinggoogle_product_categoryWe recommend including this attribute.
)
我想将其格式化为如下数组:
[warnings] => Array
(
[0] => Array
(
[domain] => Shopping
[code] => validation/missing_recommended
[location] => google_product_category
[internalReason] => We recommend including this attribute.
)
)
但所有数据似乎都嵌套在nodeValue或textContent中。
我怎么解析这个?
答案 0 :(得分:0)
您是否尝试过PHP DOMNodeList类?
此致?
答案 1 :(得分:0)
不幸的是,我不知道任何能自动将DOMNodeList解析为PHP关联数组的东西,不过我同意它会非常有用。
您需要做的是自己遍历树以获取元素,并将它们放在关联数组中。诀窍是" nodeValue"和" textContent"包含此元素中子节点的串联。这意味着您需要迭代子节点并提取所需的信息。
$warnings = array();
foreach ($elements as $element):
$warning = array();
foreach ($element->childNodes as $child):
$warning[$child->nodeName] = $child->textContent;
endforeach;
$warnings[] = $warning;
endforeach;
现在这是完全未经测试的,您必须稍微调整它以适应您需要提取的确切信息,但应该给您正确的想法。主要$child->nodeName
包含标记的名称(实际上可能不是"域"等等),$ child-> textContent在标记之间有文本(或者子节点内的文本。)