mehenky Posted May 17, 2014 Share Posted May 17, 2014 Hello, i need some help/info about searching words in files (txt or xml) and saving them to db or another file. For example ive got an xml file like below: <families> <family> <name>brown</name> <city>denver</city> <members>12</members> </family> <family> <name>jackson</name> <city>new york</city> <members>6</members> </family> </families> i want to search for: <family> then save the lines till it comes to the word </family> and then goes to the next group. did some reading about array, preg_match, strpos functions but dont know where to start. what is the best way to do this? can you guys give me some advise... or links to good tutorials? thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/ Share on other sites More sharing options...
Ch0cu3r Posted May 17, 2014 Share Posted May 17, 2014 So you're reading an XML file and wanting to add the values to a db? If so did some reading about array, preg_match, strpos functions but dont know where to start. You're barking the wrong tree there. You need to use the DOM Object Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1479875 Share on other sites More sharing options...
mehenky Posted May 17, 2014 Author Share Posted May 17, 2014 (edited) ah okay. yes i want to add the values to a db. im new to php so im looking for the easiest. i will have a look at it. thanks for the quick reply. Edited May 17, 2014 by mehenky Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1479876 Share on other sites More sharing options...
Adam Posted May 18, 2014 Share Posted May 18, 2014 SimpleXML would probably be easier to work with if you're just learning. Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1479887 Share on other sites More sharing options...
mehenky Posted May 18, 2014 Author Share Posted May 18, 2014 thanks adam, i think this is what i need. Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1479917 Share on other sites More sharing options...
mehenky Posted May 18, 2014 Author Share Posted May 18, 2014 im following the basic steps in: http://uk3.php.net/manual/en/simplexml.examples-basic.php that is working good.but now ive made this just to view: <?php $file = simplexml_load_file('stats.xml'); $check = new SimpleXMLElement($file); echo $check->counts[0]->total; ?> but im getting this error: Warning: SimpleXMLElement::__construct(): Entity: line 16: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\simplexml.php on line 3Warning: SimpleXMLElement::__construct(): in C:\xampp\htdocs\simplexml.php on line 3Warning: SimpleXMLElement::__construct(): ^ in C:\xampp\htdocs\simplexml.php on line 3Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\xampp\htdocs\simplexml.php:3 Stack trace: #0 C:\xampp\htdocs\simplexml.php(3): SimpleXMLElement->__construct('\n \n \n \n \n ...') #1 {main} thrown in C:\xampp\htdocs\simplexml.php on line 3 what is wrong with the code? below how the xml looks like. <?xml version="1.0" encoding="UTF-8" standalone="true"?> <stats> <generated>140510</generated> <u id="2"> <user>meh</user> </u> <counts> <total>156715</total> <online> <now>125</now> <today>1111</today> <week>1576</week> </online> <registrations> <today>206</today> <week>505</week> </registrations> </counts> </stats> Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1479976 Share on other sites More sharing options...
Barand Posted May 19, 2014 Share Posted May 19, 2014 should be $check = simplexml_load_file('stats.xml'); echo $check->counts[0]->total; Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1480024 Share on other sites More sharing options...
mehenky Posted May 19, 2014 Author Share Posted May 19, 2014 Thanks. Gonna test it. Quote Link to comment https://forums.phpfreaks.com/topic/288570-search-words-in-xml-file-and-save-to-db/#findComment-1480092 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.