簡體   English   中英

幫助在php中使用xmlreader讀取嵌套的xml

[英]help in reading nested xml using xmlreader in php

<root>
  <thing>
    <specs>
      <spec1 />
      <spec3 />
      <spec2 />
    </specs>
    <details />
    <more_info>
      <info1 />
      <info2 />
    </more_info>
  </thing>
</root>


okeee所以我得到了這個樣本xml,問題是我似乎無法獲取innerxml的值,
當我使用$reader->readInnerXML()它會返回整個字符串,但我確定我的xml是有效的
我想要的是分別獲得spec1,spec2,spec3的值

代碼很長,所以我在這里發布
我已經堅持了3天,現在T_T讓我很糟糕,我很樂意接受任何更正

這取決於你的“價值”。 如果你有類似的東西

<spec3 />Value</spec3>

然后readInnerXML應該給你你的價值。

如果您的值在屬性中,

<spec1 foo="my attribute" />

您需要使用XMLReader對象的getAttribute方法,或者明確告訴讀者開始解析屬性。 有關完成此操作的幾種方法,請參閱下面的代碼示例。

最后,如果節點包含更多嵌套XML,

<spec2><foo><baz thing="la de da">Value</baz></foo></spec2>

在那個時刻,讀者無法理解其中的價值/元素。 您需要執行以下操作之一

  1. 更改讀者解析代碼以掛鈎到這些深度的元素
  2. 從readInnerXML獲取XML塊並開始使用第二個XMLReader實例解析它,
  3. 從readInnerXML獲取XML塊並開始使用另一個XML解析庫解析它。

這是解析屬性的一些示例代碼

$reader = new XMLReader();
$reader->xml(trim('
<root>
  <thing>
    <specs>
      <spec1 foo="my attribute">Value</spec1>
      <spec3>
      My Text
      </spec3>
      <spec2 foo="foo again" bar="another attribute" baz="yet another attribute" />
    </specs>
    <details />
    <more_info>
      <info1 />
      <info2 />
    </more_info>
  </thing>
</root> 
'));

$last_node_at_depth = array();
$already_processed  = array();
while($reader->read()){
    $last_node_at_depth[$reader->depth] = $reader->localName;
    if(
    $reader->depth > 0 && 
    $reader->localName != '#text' &&   
    $last_node_at_depth[($reader->depth-1)] == 'specs' &&
    !in_array ($reader->localName,$already_processed)
    ){          
        echo "\n".'Processing ' . $reader->localName . "\n";
        $already_processed[] = $reader->localName;
        echo '--------------------------------------------------'."\n";
        echo 'The Value for the inner node ';           
        echo ' is [';
        echo trim($reader->readInnerXML());
        echo ']'."\n";

        if($reader->attributeCount > 0){
            echo 'This node has attributes, lets process them' . "\n";

            //grab attribute by name
            echo '    Value of attribute foo: ' . $reader->getAttribute('foo') . "\n";

            //or use the reader to itterate through all the attributes
            $length = $reader->attributeCount;
            for($i=0;$i<$length;$i++){
                //now the reader is pointing at attributes instead of nodes
                $reader->moveToAttributeNo($i);
                echo '    Value of attribute ' . $reader->localName;
                echo ': ';
                echo $reader->value;
                echo "\n";
            }
        }
        //echo $reader->localName . "\n";        
    }        
}

是廣告宣傳的

readInnerXML

讀取當前節點的內容,包括子節點和標記。

我認為你的混淆可能在節點和屬性之間。 <spec1 />不是屬性 - 它是沒有任何子節點的節點。 <spec1 />只是<spec1></spec1>簡寫。 所以你需要的是使用實際屬性:

<root>
  <thing>
    <specs spec1="" spec3="" spec2="" />
    <details />
    <more_info info1="" info2="" />
  </thing>
</root>

或者讀取那些節點。

無論如何。 我不知道這是否只是因為你向我們展示一些示例代碼或沒有,但命名節點spec1spec2spec3等可能不是一個好主意。 節點名稱在XML中不需要是唯一的。

不確定這是否是您所要求的,但simplexml可用於讀取xml數據(每個元素的值,而不是屬性)。 對於你的東西/規格示例,這樣做:

$xmlobj = simplexml_load_file($xmlfile);
$extracteddata = $xmlobj->thing->specs->spec1;

會給你spec1元素的內容。

例如:如果元素<spec1>1234</spec1> ,上面的代碼將返回“1234”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM