8, September 2008

pulling data from xml file - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: pulling data from xml file
« previous next »
Pages: [1] Print

Author 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......

<CREDIT_LIABILITY CreditLiabilityID="TRD0000" BorrowerID="100252" CreditFileID="B-EFX-01" CreditTradeReferenceID="CTR0000" _AccountIdentifier="N/A" _AccountOpenedDate="1997-07" _AccountOwnershipType="Individual" _AccountReportedDate="2001-11" _AccountStatusDate="2001-11" _AccountStatusType="Open" _AccountType="Revolving" _DerogatoryDataIndicator="N" _HighCreditAmount="6000" _LastActivityDate="2001-10" _MonthlyPaymentAmount="58" _MonthsReviewedCount="47" _TermsDescription="MONTHLY" _TermsSourceType="Provided" _UnpaidBalanceAmount="5157" CreditLoanType="UnknownLoanType">
<_CREDITOR _Name="CITI" _StreetAddress="P.O. BOX 6500" _City="SIOU FALLS" _State="SD" _PostalCode="57117">
</_CREDITOR>
<_CURRENT_RATING _Code="1" _Type="AsAgreed"/>
<_LATE_COUNT _30Days="0" _60Days="0" _90Days="0"/>
<CREDIT_COMMENT>
<_Text>AMT IN HIGH CREDIT IS CREDIT LIMIT</_Text>
</CREDIT_COMMENT>
<CREDIT_REPOSITORY _SourceType="Equifax" _SubscriberCode="906BB00289"/>
</CREDIT_LIABILITY>

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: Male
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


Last blog : Is your website is down? Know before your visitors do!
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #2 on: Feb 25, 2008, 05:59:21 PM »

i un -commented the domxml.dll in my php.ini file sine it wasn't turned on, but now it doesn't seem to want to open my xml file here's my code:

<?php

$CreditResponse = "XMLFiles/".$xmlfileName."Response.xml";
         
$fp1 = fopen($CreditResponse, "w");
         
$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: Male
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


Last blog : Is your website is down? Know before your visitors do!
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

<?xml version="1.0"?>
<title name="The place"></title>

The if statement still fails on even this simple xml file....what could be the issue...something that needs to be setup? I'm lost
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8037
41179 credits
Members referred : 3



« Reply #6 on: Feb 25, 2008, 09:55:27 PM »

Have you tried to insert the third parameter in the domxml_open_file() function to see what errors are coming back?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #7 on: Feb 26, 2008, 12:21:45 AM »

Tried this:
<?php

if (!$dom = domxml_open_file($newstring, &$error)) {
//echo "Error while parsing the document\n";
echo print_r($error);
exit;
}

?>

All the screen showed was 01?....No clue what thats supposed to mean, what am I doing wrong?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6309
38674 credits
Members referred : 374


It's time to use PHP5!


« Reply #8 on: Feb 26, 2008, 12:29:35 AM »

do you tried the example from the php manual?

<?php

if (!$dom = domxml_open_file("example.xml")) {
echo "Error while parsing the document\n";
exit;
}

$root = $dom->document_element();
?>


Last blog : Is your website is down? Know before your visitors do!
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #9 on: Feb 26, 2008, 12:41:58 AM »

i tried using the xml provided on the domxml page for their example

"<?xml version='1.0' standalone='yes'?>
<!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
[ <!ENTITY sp \"spanish\">
]>
<!-- lsfj  -->
<chapter language='en'><title language='en'>Title</title>
<para language='ge'>
  &sp;
  <!-- comment -->
  <informaltable ID='findme' language='&sp;'>
   <tgroup cols='3'>
    <tbody>
     <row><entry>a1</entry><entry
morerows='1'>b1</entry><entry>c1</entry></row>
<row><entry>a2</entry><entry>c2</entry></row>
     <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
    </tbody>
   </tgroup>
  </informaltable>
</para>
</chapter>

using

if (!$dom = domxml_open_file("example.xml")) {
echo "Error while parsing the document\n";
//echo print_r($error);
exit;
}

The if statement still failed
     
         $root = $dom->document_element();
Sandwich Artist
*
Posts: 25
186 credits
Members referred : 0


« Reply #10 on: Feb 26, 2008, 12:59:55 AM »

Ok the issue ended up being the path, I found something on another site that ended up working. here's what it looks like now.

<?php


$newstring=utf8_encode($theFile); 
$xmlPath = realpath("XMLFiles/"); 
   
$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

$creditScores = $root->get_elements_by_tagname("CREDIT_SCORE");
for($i = 0; $i<count($creditScores); $i++)
{
$node = $creditScores[$i];
      
$BorID = $node->get_attribute("BorrowerID");
$CreditScoreExp = $node->get_attribute("_Value");
$ModelName = $node->get_attribute("_ModelNameType");
         
if($BorID == 'BorRec0001')
{
Update Borrower
}
else
{
Update CoBorrower      
}
            
}//End for creditScores

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: Male
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?

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7606
Tags : php reading xml preg_match_all Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: pulling data from xml file
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep 08, 2008, 03:33:53 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.302
Total Topics: 7.479
Total Members: 3.907
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: phpprofit

14 Guests, 3 Users online :

6 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.