2013-08-22 54 views
0

我已經被從谷歌返回以下的DOMNodeList對象,我需要通過它來解析。解析的DOMNodeList成數據我可以很容易地格式化PHP

我解析成一個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. 
      ) 
    ) 

但是,所有這些數據似乎是neste d放入nodeValue或textContent中。

如何赫克我分析了這一點?

+0

http://php.net/DOMElement#86596 –

回答

0

你嘗試的PHP的DOMNodeList類?

問候?

+0

是的,顯然我沒有得到如何使用它與什麼是返回的上下文。 –

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包含標籤之間的文本(或子節點內的文本串聯)。

相關問題