2014-11-12 1 views
0

XMLReader를 사용하여 필요한 정보를 추출하기로 결정한 xml 파일의 크기가 거의 200MB입니다. 난 단지 제조 업체는 "블루 위젯"입니다 사람들의 정보가 필요,이 큰 파일의 레코드 수천의이 큰 xml 파일을 XMLreader로 어떻게 파싱합니까?

<product> 
    <manufacturer>Blue Widgets</manufacturer> 
    <price>6.79</price> 
    <condition>new</condition> 
</product> 
<product> 
    <manufacturer>Green Widgets</manufacturer> 
    <price>9.99</price> 
    <condition>new</condition> 
</product> 
<product> 
    <manufacturer>Black Widgets</manufacturer> 
    <price>3.29</price> 
    <condition>new</condition> 
</product> 
<product> 
    <manufacturer>Blue Widgets</manufacturer> 
    <price>14.99</price> 
    <condition>new</condition> 
</product> 

하지만, I : 여기

이 파일에서 XML의 조각입니다

$reader = new XMLReader(); 
$reader->open('test.xml'); 

$products = array(); 

while($reader->read()){ 

     if($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'manufacturer'){ 
     $reader->read(); 
     if($reader->value == 'Blue Widgets'){ 
      //Now that we know the manufacturer is right, how do I advance to the next price node within this product element? 
      if($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'price'){ 
       $reader->read(); 

       $products['price'] = $reader->value 
     } 
     } 
    } 
    } 

답변

1

당신은 XMLReader::next 방법을 사용할 수 있습니다 : 문제가 아니라 제조업체를 분리하는 방법을 알아내는 데입니다.

... 
$reader->read(); 
if ($reader->value == 'Blue Widgets') { 
    $reader->next('price'); // Advances cursor to the price node 
} 
... 

문서 : http://php.net/manual/en/xmlreader.next.php

귀하의 예제를 사용하여
관련 문제