Sublime directory Surf the web anonymous Pagerank Monitor


pulling data from xml file

msthac01
Fri 22 February 2008, 11:29 pm GMT +0100
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?

olaf
Sat 23 February 2008, 03:24 pm GMT +0100
I suggest to use the php xml DOM functions, start playing with the examples provided by the php manual

msthac01
Mon 25 February 2008, 04:59 pm GMT +0100
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

olaf
Mon 25 February 2008, 06:49 pm GMT +0100
I guess the xml file doesn't have a valid XML structure

msthac01
Mon 25 February 2008, 07:48 pm GMT +0100
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...

msthac01
Mon 25 February 2008, 07:57 pm GMT +0100
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

Nikolas
Mon 25 February 2008, 08:55 pm GMT +0100
Have you tried to insert the third parameter in the domxml_open_file() function to see what errors are coming back?

msthac01
Mon 25 February 2008, 11:21 pm GMT +0100
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?

olaf
Mon 25 February 2008, 11:29 pm GMT +0100
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();
?>

msthac01
Mon 25 February 2008, 11:41 pm GMT +0100
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();

msthac01
Mon 25 February 2008, 11:59 pm GMT +0100
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

msthac01
Mon 12 May 2008, 11:50 pm GMT +0200
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.

Nikolas
Tue 13 May 2008, 11:26 am GMT +0200
I am not sure about this. Have you tried to var_dump() the data you get?

Archive for SMF v1.00 by N.P. Valid XHTML 1.0 Transitional