Note that there are some explanatory texts on larger screens.

plurals
  1. POphp DOM xml parsing geon awry
    text
    copied!<p>I can't seem to parse my xml today. What have I done wrong? I'm hoping someone can point me to a better/working solution.</p> <p>I want to parse a bus route xml file that looks like this:</p> <pre><code>&lt;buses&gt; &lt;bus&gt; &lt;num&gt;12&lt;/num&gt; &lt;stime&gt;05:00&lt;/stime&gt; &lt;etime&gt;22:00&lt;/etime&gt; &lt;freq&gt;?&lt;/freq&gt; &lt;route&gt; &lt;stop&gt;Eonyang Bus Terminal&lt;/stop&gt; &lt;stop&gt;Chuk Hyub&lt;/stop&gt; &lt;stop&gt;Eonyang Bolim Hospital&lt;/stop&gt; &lt;stop&gt;Samnammyeong Residential Center&lt;/stop&gt; &lt;stop&gt;SinAn&lt;/stop&gt; &lt;/route&gt; &lt;/bus&gt; &lt;bus&gt; &lt;num&gt;13&lt;/num&gt; &lt;stime&gt;?&lt;/stime&gt; &lt;etime&gt;?&lt;/etime&gt; &lt;freq&gt;?&lt;/freq&gt; &lt;route&gt; &lt;stop&gt;KTX Ulsan Station&lt;/stop&gt; &lt;stop&gt;Driving Hagwon&lt;/stop&gt; &lt;stop&gt;Jayeon Science High School&lt;/stop&gt; &lt;stop&gt;Eonyang Bus Stop&lt;/stop&gt; &lt;stop&gt;Bolim Hospital&lt;/stop&gt; &lt;stop&gt;Daewon Green Apt.&lt;/stop&gt; &lt;/route&gt; </code></pre> <p> </p> <p>Based on user input of origin and destination, I want to search through the list of stops to find the desired bus or buses, but simply parsing this list is not working. </p> <pre><code>&lt;?php $xmlDoc = new DOMDocument(); $xmlDoc-&gt;load("buses-new.xml"); $buses=$xmlDoc-&gt;getElementsByTagName('bus'); for($i=0; $i&lt;($buses-&gt;length); $i++) { $route=$buses-&gt;item($i)-&gt;getElementsByTagName('route'); $busnum=$buses-&gt;item($i)-&gt;getElementsByTagName('num'); foreach( $route-&gt;childNodes as $stop) { $loc = $stop-&gt;nodeValue; if (stristr($loc,$orig)) { // found a bus with origin $origins[$ocount]= $i; $ocount++; } if (stristr($loc,$dest)) { // found a bus with destination $destination[$dcount] = $i; $dcount++; } } // for all stops } // for all buses </code></pre> <p>I'm getting an error on ---- foreach( $route->childNodes as $stop) -- says in invalid argument.</p> <p>what is the right way to parse an indeterminate list of childnodes from each route?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload