sdowney1 Posted February 21, 2011 Share Posted February 21, 2011 first off I have to make an empty loc.xml file with permissions in order to write contents, I understand the permission idea. secondly, simplexml_load_file does nothing I get it straight from here http://us2.php.net/manual/en/function.simplexml-load-file.php <?php $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; //echo $request; $filename = dirname(__FILE__)."/loc.xml"; $raw_xml = file_get_contents($request); //echo $raw_xml; $fp = fopen($filename, "w"); fwrite($fp, $raw_xml); fclose ($fp); // The file test.xml contains an XML document with a root element // and at least an element /[root]/title. if (file_exists($filename)) { $xml = simplexml_load_file($filename); print_r($xml); } else { exit('Failed to open loc.xml.'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/ Share on other sites More sharing options...
BlueSkyIS Posted February 21, 2011 Share Posted February 21, 2011 how do you know simplexml_load_file() does nothing? any reason you don't grab the xml without writing it to file first? $xml = simplexml_load_file($request); Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177622 Share on other sites More sharing options...
beegro Posted February 21, 2011 Share Posted February 21, 2011 First, if a function is "doing nothing" it probably means there are errors. Do you have error_reporting turned on? Second, if simplexml_load_file() isn't working as expected but you can verify that the script is getting to the function call it probably means that it is actually returning FALSE because it could not load the XML document. If the XML document indeed exists it may not be valid XML. Can you post an example of what the XML doc looks like? Also, try a var_dump() instead of the print_r(). This will tell you see if the contents of $xml is a boolean value. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177623 Share on other sites More sharing options...
sdowney1 Posted February 21, 2011 Author Share Posted February 21, 2011 here is the xml file returned from LOC <?xml version="1.0"?> <zs:searchRetrieveResponse xmlns:zs="http://www.loc.gov/zing/srw/"><zs:version>1.1</zs:version><zs:numberOfRecords>2144</zs:numberOfRecords><zs:records><zs:record><zs:recordSchema>info:srw/schema/1/marcxml-v1.1</zs:recordSchema><zs:recordPacking>xml</zs:recordPacking><zs:recordData><record xmlns="http://www.loc.gov/MARC21/slim"> <leader>01026ngm a22002773a 4500</leader> <controlfield tag="001">16429180</controlfield> <controlfield tag="005">20100823131409.0</controlfield> <controlfield tag="007">vffcjaho|</controlfield> <controlfield tag="008">100823s2010 xxu060 mleng </controlfield> <datafield tag="906" ind1=" " ind2=" "> <subfield code="a">0</subfield> <subfield code="b">cbc</subfield> <subfield code="c">orignew</subfield> <subfield code="d">u</subfield> <subfield code="e">ncip</subfield> <subfield code="f">20</subfield> <subfield code="g">y-movingim</subfield> </datafield> <datafield tag="955" ind1=" " ind2=" "> <subfield code="b">qm12 2010-08-23</subfield> </datafield> <datafield tag="010" ind1=" " ind2=" "> <subfield code="a"> 2010608899</subfield> </datafield> <datafield tag="017" ind1=" " ind2=" "> <subfield code="a">PA0001684303</subfield> <subfield code="b">U.S. Copyright Office</subfield> </datafield> <datafield tag="040" ind1=" " ind2=" "> <subfield code="a">DLC</subfield> <subfield code="c">DLC</subfield> <subfield code="e">amim</subfield> </datafield> <datafield tag="050" ind1="0" ind2="0"> <subfield code="a">VBU 4599 (viewing copy)</subfield> </datafield> <datafield tag="245" ind1="0" ind2="0"> <subfield code="a">30 Rock.</subfield> <subfield code="p">Emmanuelle goes to Dinosaur Land.</subfield> </datafield> <datafield tag="246" ind1="3" ind2="0"> <subfield code="a">Emmanuelle goes to Dinosaur Land</subfield> </datafield> <datafield tag="246" ind1="3" ind2=" "> <subfield code="a">Thirty rock.</subfield> <subfield code="p">Emmanuelle goes to Dinosaur Land</subfield> </datafield> <datafield tag="257" ind1=" " ind2=" "> <subfield code="a">United States.</subfield> </datafield> <datafield tag="260" ind1=" " ind2=" "> <subfield code="c">2010-05-13.</subfield> </datafield> <datafield tag="300" ind1=" " ind2=" "> <subfield code="a">1 videocassette of 1 (Betacam SP) (60 min.) :</subfield> <subfield code="b">sd., col. ;</subfield> <subfield code="c">1/2 in.</subfield> <subfield code="3">viewing copy.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Episode no. 4021.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Sources used: videocassette container; Copyright catalog online; Copyright description.</subfield> </datafield> <datafield tag="655" ind1=" " ind2="0"> <subfield code="a">Situation comedies (Television programs)</subfield> </datafield> <datafield tag="655" ind1=" " ind2="0"> <subfield code="a">Fiction television programs.</subfield> </datafield> <datafield tag="710" ind1="2" ind2=" "> <subfield code="a">Copyright Collection (Library of Congress)</subfield> <subfield code="5">DLC</subfield> </datafield> </record></zs:recordData><zs:recordPosition>2</zs:recordPosition></zs:record><zs:record><zs:recordSchema>info:srw/schema/1/marcxml-v1.1</zs:recordSchema><zs:recordPacking>xml</zs:recordPacking><zs:recordData><record xmlns="http://www.loc.gov/MARC21/slim"> <leader>01718cjm a22003971a 4500</leader> <controlfield tag="001">13463061</controlfield> <controlfield tag="005">20051217130827.0</controlfield> <controlfield tag="007">sd fsngnnmmned</controlfield> <controlfield tag="008">040120r19961983caurcn eng d</controlfield> <datafield tag="024" ind1="1" ind2=" "> <subfield code="a">076744000422</subfield> </datafield> <datafield tag="035" ind1=" " ind2=" "> <subfield code="a">(DLC) 2004567544</subfield> </datafield> <datafield tag="040" ind1=" " ind2=" "> <subfield code="a">KFW</subfield> <subfield code="c">KFW</subfield> <subfield code="d">IEP</subfield> <subfield code="d">OCLCQ</subfield> <subfield code="d">DLC</subfield> </datafield> <datafield tag="020" ind1=" " ind2=" "> <subfield code="c">$17.98</subfield> </datafield> <datafield tag="024" ind1="1" ind2="0"> <subfield code="a">076744000422</subfield> </datafield> <datafield tag="028" ind1="0" ind2="2"> <subfield code="a">HIPD 40004</subfield> <subfield code="b">Hip-O Records</subfield> </datafield> <datafield tag="028" ind1="0" ind2="2"> <subfield code="a">40004-2</subfield> <subfield code="b">Hip-O Records</subfield> </datafield> <datafield tag="035" ind1=" " ind2=" "> <subfield code="a">(OCoLC)ocm35640234 </subfield> </datafield> <datafield tag="028" ind1="0" ind2="2"> <subfield code="a">HIPD-40004</subfield> <subfield code="b">Hip-O Records</subfield> </datafield> <datafield tag="010" ind1=" " ind2=" "> <subfield code="a"> 2004567544</subfield> </datafield> <datafield tag="042" ind1=" " ind2=" "> <subfield code="a">lcderive</subfield> </datafield> <datafield tag="050" ind1="0" ind2="0"> <subfield code="a">SDA 85496</subfield> </datafield> <datafield tag="245" ind1="0" ind2="4"> <subfield code="a">The '80s hit(s) back!</subfield> <subfield code="h">[sound recording].</subfield> </datafield> <datafield tag="246" ind1="3" ind2=" "> <subfield code="a">Eighty's hit(s) back!</subfield> </datafield> <datafield tag="260" ind1=" " ind2=" "> <subfield code="a">Universal City, Calif. :</subfield> <subfield code="b">Hip-O Records,</subfield> <subfield code="c">p1996.</subfield> </datafield> <datafield tag="300" ind1=" " ind2=" "> <subfield code="a">1 sound disc :</subfield> <subfield code="b">digital ;</subfield> <subfield code="c">4 3/4 in.</subfield> </datafield> <datafield tag="511" ind1="0" ind2=" "> <subfield code="a">Various performers.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Selections previously released 1983-1988.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Compact disc.</subfield> </datafield> <datafield tag="505" ind1="0" ind2=" "> <subfield code="a">She drives me crazy (Fine Young Cannibals) -- Walk the dinosaur (Was (Not Was)) -- You keep me hangin' on (Kim Wilde) -- The safety dance (Men Without Hats) -- Walking on sunshine (Katrina & The Waves) -- One thing leads to another (The Fixx) -- Heaven is a place on earth (Belinda Carlisle) -- Everybody have fun tonight (Wang Chung) -- Cruel summer (Bananarama) -- Weird science (Oingo Boingo) -- Axel F (Harold Faltermeyer) -- The future's so bright, I gotta wear shades (Timbuk 3).</subfield> </datafield> <datafield tag="650" ind1=" " ind2="0"> <subfield code="a">Rock music</subfield> <subfield code="y">1981-1990.</subfield> </datafield> <datafield tag="655" ind1=" " ind2="7"> <subfield code="a">Compact discs.</subfield> <subfield code="2">lcsh</subfield> </datafield> <datafield tag="906" ind1=" " ind2=" "> <subfield code="a">7</subfield> <subfield code="b">cbc</subfield> <subfield code="c">copycat</subfield> <subfield code="d">3</subfield> <subfield code="e">ncip</subfield> <subfield code="f">20</subfield> <subfield code="g">y-genmusic</subfield> </datafield> <datafield tag="925" ind1="0" ind2=" "> <subfield code="a">acquire</subfield> <subfield code="b">2 copies</subfield> <subfield code="x">policy default</subfield> </datafield> <datafield tag="952" ind1=" " ind2=" "> <subfield code="a">muzerec</subfield> </datafield> <datafield tag="955" ind1=" " ind2=" "> <subfield code="a">vn76 2004-01-20 to MBRS/RS</subfield> <subfield code="e">vn76 2004-01-20 copy 2 to MBRS/RS</subfield> </datafield> <datafield tag="985" ind1=" " ind2=" "> <subfield code="c">OCLC</subfield> <subfield code="e">srreplace 2005-08</subfield> </datafield> </record></zs:recordData><zs:recordPosition>3</zs:recordPosition></zs:record><zs:record><zs:recordSchema>info:srw/schema/1/marcxml-v1.1</zs:recordSchema><zs:recordPacking>xml</zs:recordPacking><zs:recordData><record xmlns="http://www.loc.gov/MARC21/slim"> <leader>01461ngm a22003375a 4500</leader> <controlfield tag="001">11624471</controlfield> <controlfield tag="005">00000000000000.0</controlfield> <controlfield tag="008">930921s1967 xxu vaeng </controlfield> <datafield tag="035" ind1=" " ind2=" "> <subfield code="9">(DLC) 93513624</subfield> </datafield> <datafield tag="906" ind1=" " ind2=" "> <subfield code="a">0</subfield> <subfield code="b">ibc</subfield> <subfield code="c">orignew</subfield> <subfield code="d">u</subfield> <subfield code="e">ncip</subfield> <subfield code="f">19</subfield> <subfield code="g">y-movingim</subfield> </datafield> <datafield tag="010" ind1=" " ind2=" "> <subfield code="a"> 93513624 </subfield> </datafield> <controlfield tag="007">v| ||||||</controlfield> <datafield tag="017" ind1=" " ind2=" "> <subfield code="a">PA608-254</subfield> <subfield code="b">U.S. Copyright Office</subfield> </datafield> <datafield tag="040" ind1=" " ind2=" "> <subfield code="a">DLC</subfield> <subfield code="c">DLC</subfield> <subfield code="e">amim</subfield> </datafield> <datafield tag="050" ind1="0" ind2="0"> <subfield code="a">VBK 2042 (viewing copy)</subfield> </datafield> <datafield tag="245" ind1="0" ind2="0"> <subfield code="a">Abbott & Costello cartoons.</subfield> <subfield code="p">Dinosaur Dilemna /</subfield> <subfield code="c">a Hanna-Barbera Production in association with RKO Pictures Company-Jomar Productions ; directed and produced by Joseph Barbera and William Hanna.</subfield> </datafield> <datafield tag="260" ind1=" " ind2=" "> <subfield code="a">United States :</subfield> <subfield code="b">[s.n.],</subfield> <subfield code="c">1967.</subfield> </datafield> <datafield tag="300" ind1=" " ind2=" "> <subfield code="a">1 videocassette of 1 :</subfield> <subfield code="b">sd., col. ;</subfield> <subfield code="c">3/4 in. viewing copy.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Copyright: PUB 5May67; PA608-254.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Copyright notice on film: RKO General Inc., Jomar Prod. Inc., Hanna-Barbera Productions, Inc. ; 1967.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">On cassette with episodes: Frigid fugitive ; Invader raider ; Paddleboat pirate.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Number 12</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Animation.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Source used: copyright data sheet.</subfield> </datafield> <datafield tag="541" ind1=" " ind2=" "> <subfield code="d">Received: 4/27/1993;</subfield> <subfield code="3">viewing copy;</subfield> <subfield code="c">copyright deposit--RNR;</subfield> <subfield code="a">Copyright Collection.</subfield> </datafield> <datafield tag="710" ind1="2" ind2=" "> <subfield code="a">Copyright Collection (Library of Congress)</subfield> <subfield code="5">DLC</subfield> </datafield> <datafield tag="740" ind1="0" ind2=" "> <subfield code="a">Dinosaur dilemna.</subfield> </datafield> <datafield tag="740" ind1="0" ind2=" "> <subfield code="a">Abbott and Costello cartoons.</subfield> <subfield code="p">Dinosaur dilemna.</subfield> </datafield> <datafield tag="953" ind1=" " ind2=" "> <subfield code="a">TE01</subfield> </datafield> <datafield tag="969" ind1=" " ind2=" "> <subfield code="a">qxp</subfield> </datafield> <datafield tag="991" ind1=" " ind2=" "> <subfield code="b">c-MP&TV</subfield> <subfield code="h">VBK 2042 (viewing copy)</subfield> <subfield code="w">MUMS VM File</subfield> </datafield> </record></zs:recordData><zs:recordPosition>4</zs:recordPosition></zs:record><zs:record><zs:recordSchema>info:srw/schema/1/marcxml-v1.1</zs:recordSchema><zs:recordPacking>xml</zs:recordPacking><zs:recordData><record xmlns="http://www.loc.gov/MARC21/slim"> <leader>01461ngm a22003375a 4500</leader> <controlfield tag="001">11624468</controlfield> <controlfield tag="005">00000000000000.0</controlfield> <controlfield tag="008">930921s1967 xxu vaeng </controlfield> <datafield tag="035" ind1=" " ind2=" "> <subfield code="9">(DLC) 93513621</subfield> </datafield> <datafield tag="906" ind1=" " ind2=" "> <subfield code="a">0</subfield> <subfield code="b">ibc</subfield> <subfield code="c">orignew</subfield> <subfield code="d">u</subfield> <subfield code="e">ncip</subfield> <subfield code="f">19</subfield> <subfield code="g">y-movingim</subfield> </datafield> <datafield tag="010" ind1=" " ind2=" "> <subfield code="a"> 93513621 </subfield> </datafield> <controlfield tag="007">v| ||||||</controlfield> <datafield tag="017" ind1=" " ind2=" "> <subfield code="a">PA608-252</subfield> <subfield code="b">U.S. Copyright Office</subfield> </datafield> <datafield tag="040" ind1=" " ind2=" "> <subfield code="a">DLC</subfield> <subfield code="c">DLC</subfield> <subfield code="e">amim</subfield> </datafield> <datafield tag="050" ind1="0" ind2="0"> <subfield code="a">VBK 2042 (viewing copy)</subfield> </datafield> <datafield tag="245" ind1="0" ind2="0"> <subfield code="a">Abbott & Costello cartoons.</subfield> <subfield code="p">Frigid fugitive /</subfield> <subfield code="c">a Hanna-Barbera Production in association with RKO Pictures Company-Jomar Productions ; directed and produced by Joseph Barbera and William Hanna.</subfield> </datafield> <datafield tag="260" ind1=" " ind2=" "> <subfield code="a">United States :</subfield> <subfield code="b">[s.n.],</subfield> <subfield code="c">1967.</subfield> </datafield> <datafield tag="300" ind1=" " ind2=" "> <subfield code="a">1 videocassette of 1 :</subfield> <subfield code="b">sd., col. ;</subfield> <subfield code="c">3/4 in. viewing copy.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Copyright: PUB 12May67; PA608-252.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Copyright notice on film: RKO General Inc., Jomar Prod. Inc., Hanna-Barbera Productions, Inc. ; 1967.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">On cassette with episodes: Invader raider ; Dinosaur dilemna ; Paddleboat pirate.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Number 12.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Animation.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Source used: copyright data sheet.</subfield> </datafield> <datafield tag="541" ind1=" " ind2=" "> <subfield code="d">Received: 4/27/1993;</subfield> <subfield code="3">viewing copy;</subfield> <subfield code="c">copyright deposit--RNR;</subfield> <subfield code="a">Copyright Collection.</subfield> </datafield> <datafield tag="710" ind1="2" ind2=" "> <subfield code="a">Copyright Collection (Library of Congress)</subfield> <subfield code="5">DLC</subfield> </datafield> <datafield tag="740" ind1="0" ind2=" "> <subfield code="a">Frigid fugitive.</subfield> </datafield> <datafield tag="740" ind1="0" ind2=" "> <subfield code="a">Abbott and Costello cartoons.</subfield> <subfield code="p">Frigid fugitive.</subfield> </datafield> <datafield tag="953" ind1=" " ind2=" "> <subfield code="a">TE01</subfield> </datafield> <datafield tag="969" ind1=" " ind2=" "> <subfield code="a">qxp</subfield> </datafield> <datafield tag="991" ind1=" " ind2=" "> <subfield code="b">c-MP&TV</subfield> <subfield code="h">VBK 2042 (viewing copy)</subfield> <subfield code="w">MUMS VM File</subfield> </datafield> </record></zs:recordData><zs:recordPosition>5</zs:recordPosition></zs:record><zs:record><zs:recordSchema>info:srw/schema/1/marcxml-v1.1</zs:recordSchema><zs:recordPacking>xml</zs:recordPacking><zs:recordData><record xmlns="http://www.loc.gov/MARC21/slim"> <leader>01458ngm a22003375a 4500</leader> <controlfield tag="001">11624469</controlfield> <controlfield tag="005">00000000000000.0</controlfield> <controlfield tag="008">930921s1967 xxu vaeng </controlfield> <datafield tag="035" ind1=" " ind2=" "> <subfield code="9">(DLC) 93513622</subfield> </datafield> <datafield tag="906" ind1=" " ind2=" "> <subfield code="a">0</subfield> <subfield code="b">ibc</subfield> <subfield code="c">orignew</subfield> <subfield code="d">u</subfield> <subfield code="e">ncip</subfield> <subfield code="f">19</subfield> <subfield code="g">y-movingim</subfield> </datafield> <datafield tag="010" ind1=" " ind2=" "> <subfield code="a"> 93513622 </subfield> </datafield> <controlfield tag="007">v| ||||||</controlfield> <datafield tag="017" ind1=" " ind2=" "> <subfield code="a">PA608-253</subfield> <subfield code="b">U.S. Copyright Office</subfield> </datafield> <datafield tag="040" ind1=" " ind2=" "> <subfield code="a">DLC</subfield> <subfield code="c">DLC</subfield> <subfield code="e">amim</subfield> </datafield> <datafield tag="050" ind1="0" ind2="0"> <subfield code="a">VBK 2042 (viewing copy)</subfield> </datafield> <datafield tag="245" ind1="0" ind2="0"> <subfield code="a">Abbott & Costello cartoons.</subfield> <subfield code="p">Invader raider /</subfield> <subfield code="c">a Hanna-Barbera Production in association with RKO Pictures Company-Jomar Productions ; directed and produced by Joseph Barbera and William Hanna.</subfield> </datafield> <datafield tag="260" ind1=" " ind2=" "> <subfield code="a">United States :</subfield> <subfield code="b">[s.n.],</subfield> <subfield code="c">1967.</subfield> </datafield> <datafield tag="300" ind1=" " ind2=" "> <subfield code="a">1 videocassette of 1 :</subfield> <subfield code="b">sd., col. ;</subfield> <subfield code="c">3/4 in. viewing copy.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Copyright: PUB 1Jun67; PA608-253.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Copyright notice on film: RKO General Inc., Jomar Prod. Inc., Hanna-Barbera Productions, Inc. ; 1967.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">On cassette with episodes: Frigid fugitive ; Dinosaur dilemna ; Paddleboat pirate.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Number 12.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Animation.</subfield> </datafield> <datafield tag="500" ind1=" " ind2=" "> <subfield code="a">Source used: copyright data sheet.</subfield> </datafield> <datafield tag="541" ind1=" " ind2=" "> <subfield code="d">Received: 4/27/1993;</subfield> <subfield code="3">viewing copy;</subfield> <subfield code="c">copyright deposit--RNR;</subfield> <subfield code="a">Copyright Collection.</subfield> </datafield> <datafield tag="710" ind1="2" ind2=" "> <subfield code="a">Copyright Collection (Library of Congress)</subfield> <subfield code="5">DLC</subfield> </datafield> <datafield tag="740" ind1="0" ind2=" "> <subfield code="a">Invader raider.</subfield> </datafield> <datafield tag="740" ind1="0" ind2=" "> <subfield code="a">Abbott and Costello cartoons.</subfield> <subfield code="p">Invader raider.</subfield> </datafield> <datafield tag="953" ind1=" " ind2=" "> <subfield code="a">TE01</subfield> </datafield> <datafield tag="969" ind1=" " ind2=" "> <subfield code="a">qxp</subfield> </datafield> <datafield tag="991" ind1=" " ind2=" "> <subfield code="b">c-MP&TV</subfield> <subfield code="h">VBK 2042 (viewing copy)</subfield> <subfield code="w">MUMS VM File</subfield> </datafield> </record></zs:recordData><zs:recordPosition>6</zs:recordPosition></zs:record></zs:records></zs:searchRetrieveResponse> Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177669 Share on other sites More sharing options...
sdowney1 Posted February 21, 2011 Author Share Posted February 21, 2011 $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; //echo $request; // $filename = dirname(__FILE__)."/loc.xml"; // $raw_xml = file_get_contents($request); //echo $raw_xml; // $fp = fopen($filename, "w"); // fwrite($fp, $raw_xml); // fclose ($fp); $xml = simplexml_load_file($request); var_dump ($xml); shows just 'object(SimpleXMLElement)#1 (0) { } ' Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177677 Share on other sites More sharing options...
sdowney1 Posted February 21, 2011 Author Share Posted February 21, 2011 also it returns false <?php $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; $xml = simplexml_load_file($request); if ($xml == FALSE){ echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) {echo "\t", $error->message;}//Failed loading XML was the returned error } dump_var($xml); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177688 Share on other sites More sharing options...
BlueSkyIS Posted February 21, 2011 Share Posted February 21, 2011 odd, i get the same thing. my lack of xml knowledge is showing. i am able to get the xml contents using this, though: $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; $xml = file_get_contents($request); Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177690 Share on other sites More sharing options...
sdowney1 Posted February 21, 2011 Author Share Posted February 21, 2011 forgot on previous to set errors true. but still get nothing, returns false and no errors are shown? I am pretty sure the xml is not malformed by Library of Congress. <?php libxml_use_internal_errors(true); $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; $xml = simplexml_load_file($request); if ($xml == FALSE){ echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) {echo "\t", $error->message;}//Failed loading XML was the returned error } dump_var($xml); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177697 Share on other sites More sharing options...
sdowney1 Posted February 21, 2011 Author Share Posted February 21, 2011 well, I thought perhaps a bug, but cant be submitted to php.net so likely unless no one can help, I will have to try something else WARNING: YOU MUST RE-UPLOAD YOUR PATCH, OR IT WILL BE IGNORED Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177709 Share on other sites More sharing options...
beegro Posted February 21, 2011 Share Posted February 21, 2011 It's definitely failing to load the XML despite the XML being valid. I checked the XML with W3C: http://www.w3schools.com/xml/xml_validator.asp AND I tried using the SimpleXMLElement object instead of the function call but got the same results. This may indeed be a bug but I'll try a few more things first. error_reporting(E_ALL); ini_set("display_errors", 1); $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; $string = file_get_contents($request); echo $string; $oXml = new SimpleXMLElement($string); if (!$oXml) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } } var_dump ($oXml); Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177715 Share on other sites More sharing options...
beegro Posted February 21, 2011 Share Posted February 21, 2011 OK, after further investigating the XML document is valid but PHP has trouble handling the ":" character in the tag. Here's a hack workaround but this is definitely a bug or issue with PHP. error_reporting(E_ALL); ini_set("display_errors", 1); $request='http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'; $string = file_get_contents($request); // echo $string; $string = str_replace(":", "_", $string); // echo $string; $sxe = new SimpleXMLElement($string); if (!$sxe) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } } var_dump ($sxe); You should be able to see the elements with an "_" character replacing where the ":" was previously. Hope this helps for now. Revision: I guess I shouldn't say this is a bug because the ":" character is part of the PHP language construct but it should be handled more gracefully in the SimpleXML object. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177727 Share on other sites More sharing options...
xtiancjs Posted February 21, 2011 Share Posted February 21, 2011 Just worked on something similar, try this : $url = file_get_contents('http://z3950.loc.gov:7090/voyager?version=1.1&operation=searchRetrieve&query=dinosaur&startRecord=2&maximumRecords=5'); $xml = new SimpleXMLElement($url) ; //file put contents - same as fopen, wrote and close //need to output "asXML" - simple xml returns an object based upon the raw xml file_put_contents(dirname(__FILE__)."/loc.xml", $xml->asXML()); Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177728 Share on other sites More sharing options...
xtiancjs Posted February 21, 2011 Share Posted February 21, 2011 Also, I highly discourage doing a string replace on the ":" in your returned xml. The ":" denotes the namespaces in the xml string. You will run into troubles trying to parse your file later on. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177740 Share on other sites More sharing options...
beegro Posted February 21, 2011 Share Posted February 21, 2011 I agree with xtiancjs on not making a habit of hacking XML to get it to parse with SimpleXML. I only offered the str_replace() hack since the namespace URLs within the XML resolve to 404 pages and therefor cannot be used for parsing children. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177789 Share on other sites More sharing options...
xtiancjs Posted February 21, 2011 Share Posted February 21, 2011 also, if you are just getting the xml from a remote address and you wish to just write the raw contents to a file, you don't need simple xml till later. Simple xml is for parsing and writing xml. You could also get the page contents using curl. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177856 Share on other sites More sharing options...
BlueSkyIS Posted February 21, 2011 Share Posted February 21, 2011 also, if you are just getting the xml from a remote address and you wish to just write the raw contents to a file, you don't need simple xml till later. Simple xml is for parsing and writing xml. You could also get the page contents using curl. ... or even simpler using file_get_contents() Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177879 Share on other sites More sharing options...
sdowney1 Posted February 21, 2011 Author Share Posted February 21, 2011 Thank you very much for looking into this. Could someone submit a bug report on it? I dont have time to try the fixes just yet. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177966 Share on other sites More sharing options...
sdowney1 Posted February 22, 2011 Author Share Posted February 22, 2011 well, yes, my idea was to write contents to a file. Then load it and parse it so it runs something like this LOC web site user search here. http://www.loc.gov/z3950/ My idea was to create a search page for what the user wants to find Then using the LOC SRU at http://www.loc.gov/standards/sru/ (they show some examples http://www.loc.gov/standards/sru/simple.html ), run the query and return results as XML. Save the file, getting 20 records at a time, process the xml so it looks like this http://www.loc.gov/z3950/ when they search. Forward or back just get a new XML file from LOC. Click on 'more on this record' and display the marc data. Then have a button when the user clicks loads it into a file or directly into the database. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1177968 Share on other sites More sharing options...
xtiancjs Posted February 22, 2011 Share Posted February 22, 2011 A brief overview of how I would start this. Seems like there will be multiple calls to a base url with some changing parameters. looking here: http://www.loc.gov/standards/sru/simple.html points 2, 3, and 5 seem like the way to start. So essentially you are making an initial call to give you the number of records, then the same call a bunch of times to get the next set based upon your parameters in the url. I would create a class file that deals with all this in an oop fashion. You would have a method that sends the requests, I would use curl, and a method that deals with parsing the xml - simpleXml(for the simple xml look into parsing children of namespaces in this case "zs" - simple xml can definitely parse these results just have to experiment with the correct syntax and combo of children() etc.). No need to actually write data to a file or DB until you really need to at the end for a single record. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1178019 Share on other sites More sharing options...
sdowney1 Posted February 22, 2011 Author Share Posted February 22, 2011 http://wap.w3schools.com/php/php_xml_simplexml.asp However, when dealing with advanced XML, like namespaces, you are better off using the Expat parser or the XML DOM. http://www.w3schools.com/xml/xml_namespaces.asp An XML parser will not know how to handle these differences. so should i just try and work with XML DOM methods? perhaps this : namespace issue means it is not a bug in the simple xml function although your saying it might could work. truthfully, I dont know a lot about parsing XML yet. And it does not seem very simple (simplexml) at all. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1178248 Share on other sites More sharing options...
sdowney1 Posted February 22, 2011 Author Share Posted February 22, 2011 http://blog.sherifmansour.com/?p=302 but it should still load xml with namespaces, so perhaps still a bug. Really pretty confused now. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1178253 Share on other sites More sharing options...
sdowney1 Posted February 23, 2011 Author Share Posted February 23, 2011 finally got some help here as I was truly stuck on the child node processing http://ubuntuforums.org/showthread.php?t=1693277 This following code takes an xml marc file and parses it out to match what LOC normally displays as a marc record It uses DOM but since it wont have more than 20 records it should be ok. It shows how to display the attributes associated with child element EXCEPT, I dont understand why I had to change this if(!$d->hasChildNodes) to if($d->hasChildNodes) as I thought subfield was a child node of datafield?? any ideas? <?php //http://www.phpbuilder.com/columns/DOM-XML-extension/Octavia_Anghel102710.php3 //child nodes http://www.w3schools.com/php/php_xml_dom.asp //http://books.google.com/books?id=ygQRKDEsLecC&pg=PA196&lpg=PA196&dq=print+php+xml+child+node+attributes&source=bl&ots=TCAhH4kpCt&sig=yJUPXesIkRUrNXdNE9Yr5ul7Wdw&hl=en&ei=U51kTeKQKsL48AbN4MDTBg&sa=X&oi=book_result&ct=result&resnum=10&ved=0CF4Q6AEwCQ#v=onepage&q=print%20php%20xml%20child%20node%20attributes&f=false //http://stackoverflow.com/questions/4598409/printing-content-of-a-xml-file-using-xml-dom //http://www.php.net/manual/en/book.dom.php $doc = new DOMDocument(); $doc->preserveWhiteSpace = FALSE; $doc->load( 'loc.xml' ); $root = $doc->documentElement; $type = $root->nodeType; //print $doc->nodeName."\n"; //print $root->nodeName."\n"; //print $doc->nodeValue."\n"; //print $root->nodeValue."\n"; $librecords = $doc->getElementsByTagName( "record" ); foreach( $librecords as $record ){ $leader = $record->getElementsByTagName( "leader" ); $controlfields = $record->getElementsByTagName( "controlfield" ); $datafields = $record->getElementsByTagName( "datafield" ); $leader = $leader->item(0)->nodeValue; echo '=LDR '.$leader.'<BR>'; foreach( $controlfields as $controlfield ){ $tag = $controlfield->getAttribute('tag'); $cf_value = $controlfield->firstChild->nodeValue; echo "=".$tag." ".$cf_value.'<BR>'; } // loop over each datafield foreach($datafields as $d){ $tag = $d->getAttribute('tag'); $ind1 = $d->getAttribute('ind1'); $ind2 = $d->getAttribute('ind2'); $ind1 = ($ind1 == " ") ? "_" : $ind1; $ind2 = ($ind2 == " ") ? "_" : $ind2; echo "=".$tag." ".$ind1.$ind2; // excuse the \n, using CLI if(!$d->hasChildNodes) { // select its subfields $subfields = $d->getElementsByTagName('subfield'); // loop over them foreach($subfields as $s){ $code = $s->getAttribute('code'); $value = $s->nodeValue; print '$'.$code; print $value; } } echo '<BR>'; } echo '<BR>'; echo '<BR>'; } ?> foreach( $subfields as $subfield ){ $code = $subfield->getAttribute('code'); $sf_value = $subfield->firstChild->nodeValue; echo '$'.$code.$sf_value.'<BR>'; } Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1178727 Share on other sites More sharing options...
sdowney1 Posted February 23, 2011 Author Share Posted February 23, 2011 I got ! backwards, sorry! why I had to change this if($d->hasChildNodes) to if(!$d->hasChildNodes) which seems backwards to me. anyhow, thanks for the help so far. Quote Link to comment https://forums.phpfreaks.com/topic/228387-why-does-this-simplexml_load_file-not-work/#findComment-1178730 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.