Topic: pulling data from xml file (Read 610 times)
Sandwich Artist
Posts: 25
186 credits Members referred : 0
« on: Feb 23, 2008, 12:29:44 AM »
Alright I'm at the point where I can read the data into an array, but now I'm stuck on what I thought would be the hardest part of this...basically when I receive my data it can be of varying length and tags can potentially be repeated any number of times which brings up my issue... Due to the varying length of the file I don't know how to account for this when saving the data to the SQL db. I wanted to save a particular set of tags into one table and the rest of the data into another table, but how will I account for multiple tags...here's an example:
This tag can appear anywhere from 1 to XX times and I want to save it to a separate table so as to be able to account for multiple instances of the tag...I figure that the best option would be to pull these tags into a separate file and then read and save from there, but how can I accomplish pulling just these tags out......
I think the preg_match_all function is what I'm needing in this instance, I just can't seem to figure out the right way to code the expression to get the tags I want out, and I'll also have to loop through the results somehow to make sure every occurance of the tag is saved to the db. Any one familiar with the preg_match_all function?
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6309
38674 credits Members referred : 374
It's time to use PHP5!
« Reply #1 on: Feb 23, 2008, 04:24:57 PM »
I suggest to use the php xml DOM functions, start playing with the examples provided by the php manual
$ch=curl_init(); // This is the custom header that specifies following is text/xml format. $headers = array( "Content-Type: text/xml" ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); curl_setopt($ch, CURLOPT_FILE, $fp1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds curl_setopt ($ch, CURLOPT_USERAGENT, "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $xmlResponse = curl_exec ($ch); //echo $xmlResponse; fwrite($fp1,$xmlResponse); curl_close ($ch); fclose($fp1);
if (!$dom = domxml_open_file($CreditResponse)) { echo "Error while parsing the document\n"; exit; }
$root = $dom->document_element();
I keep getting the Error while parsing the document no matter what I try in the argument, even putting the file into the same dir and putting the name "text.xml" into the (). Any ideas on why this isn't working? All of the other code works to post, receive data, and put it into the xml file
Global Moderator Community Supporter?
Jedai Sword Master
Gender:
Posts: 6309
38674 credits Members referred : 374
It's time to use PHP5!
« Reply #3 on: Feb 25, 2008, 07:49:12 PM »
I guess the xml file doesn't have a valid XML structure
Sandwich Artist
Posts: 25
186 credits Members referred : 0
« Reply #4 on: Feb 25, 2008, 08:48:48 PM »
but i was able to use an xml parser to get all of the data out of the xml file into an array. Just the fact that the array is of varying length is making me need to find a way to just pull out the tags that appear multiple times so I can save them to a separate db table. Also if I open the file in IE it displays properly as an xml file...
Sandwich Artist
Posts: 25
186 credits Members referred : 0
« Reply #5 on: Feb 25, 2008, 08:57:33 PM »
I was unable to even use the dom_xml_openfile command on a simple xml file such as
$theFile = $xmlfileName."Response.xml"; if (!$dom = domxml_open_file($xmlPath."/".$theFile)) { echo "Error while parsing the document\n"; exit; }
$root = $dom->document_element(); ?>
Now i just have to play with getting just the tags and data I want out, If I have anymore questions I'll post back. Thanks for everyones help
Sandwich Artist
Posts: 25
186 credits Members referred : 0
« Reply #11 on: May 13, 2008, 12:50:05 AM »
well after getting everything up and running, we've come to a point where I need to save some other data from the file that I'm receiving, only issue is I'm not 100% sure how to get to this data. Basically I'm pulling credit reports, and for each applicant on the report there are 3 credit agencies scoring, and with each agency they send the factors that played into the credit score. I'm already looping through and finding the credit scores and saving those the issue is that the factors are an element under the credit score and I'm not sure how to access them using domxml....here's what the xml looks like that I'm getting back
<CREDIT_SCORE CreditScoreID="CRScr0002" BorrowerID="BorRec0001" CreditFileID="" CreditReportIdentifier="" CreditRepositorySourceType="Experian" _ModelNameType="ExperianFairIsaac" _Value=""> <_FACTOR _Code="18" _Text="NUMBER OF ACCOUNTS DELINQUENT" /> <_FACTOR _Code="10" _Text="PROPORTION OF BALANCE TO HIGH CREDIT ON BANK REVOLVING OR ALL REVOLVING ACCOUNTS" /> <_FACTOR _Code="14" _Text="LENGTH OF TIME ACCOUNTS HAVE BEEN ESTABLISHED" /> <_FACTOR _Code="05" _Text="NUMBER OF ACCOUNTS WITH BALANCES" /> </CREDIT_SCORE>
and these <credit_score> tags are repeated for each borrower on the report and for each credit agency
here's how I'm accessing the credit score value as of now
So how could I access the factors while in the loop gathering each credit score? I'm not really sure how to go about this. Any help is greatly appreciated.
I am a metal monkey!
Administrator Community Supporter?
Jedai Sword Master
Gender:
Posts: 8037
41179 credits Members referred : 3
« Reply #12 on: May 13, 2008, 12:26:28 PM »
I am not sure about this. Have you tried to var_dump() the data you get?