Forum Moderators: coopster
<RESPONDING_PARTY _Name="The Co name" _StreetAddress="The Address" _City="The City" _State="The State" _PostalCode="The ZIP">
How do I go about pulling the data out of a tag laid out like this? Any suggestions
Here's the code that pulls the data:
<?php
$filename = $CreditResponse;
$fp = fopen($filename, 'rb');
$TheSize = filesize($filename);
$xml_dataRead = fread($fp, $TheSize);
//echo htmlentities($xml_dataRead);
$usercount=0;
$userdata=array();
$state='';
function startElementHandler ($parser,$name,$attrib)
{
global $usercount;
global $userdata;
global $state;
switch ($name)
{
case $name=="THIS IS NOT USED FOR GetSmart" :
{
break;
}
default : {$state=$name;break;}
}
}
function endElementHandler ($parser,$name)
{
global $usercount;
global $userdata;
global $state;
$state='';
if($name=="LeadInformation") {$usercount++;}
}
function characterDataHandler ($parser, $data)
{
global $usercount;
global $userdata;
global $state;
if (!$state) {return;}
if ($state=="CITY") { $userdata[$usercount]["city"] = $data;}
if ($state=="STATE") { $userdata[$usercount]["state"] = $data;}
} // end function
if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
while($data = fread($fp, $TheSize))
{
if(!xml_parse($xml_parser, $data, feof($fp)))
{
break;
}
}
fclose($fp);
xml_parser_free($xml_parser);
for ($i=0;$i<=$usercount; $i++)
{
mysql_select_db($database_dbConnect, $dbConnect);
$city = addslashes(trim($userdata[$i]["city"]));
$state = addslashes(trim($userdata[$i]["state"]));
?>
So would it be possible to modify this code to handle the fact that there is more data within the tags and to pull that data out?
The code below is mainly copied from some code that I use. So there may well be bits that you dont need, but its late so this is a quick reply ;)
$_parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option($_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($_parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_object($_parser, $this);
xml_set_element_handler($_parser, 'tag_open', 'tag_close');
// xml_set_character_data_handler($_parser, 'tag_contents');
function tag_open ($parser, $tag, $attr) {
switch ($tag) {
case 'RESPONDING_PARTY':
// you can access the attributes here
foreach ($attr as $key => $value) {
echo "$key : $value<br />\n";
}
break;
default:
// whatever
break;
}
}
// you need to supply a tag_close function
The details are on the xml_set_element_handler [uk2.php.net] page.
PHP4 sucks for XML, but you can still work with everything. Its just a lot more bother than it is with PHP5.
[php
$file = $filename;
$depth = 0;
$tree = array();
$tree['name'] = "root";
$stack[] = &$tree;
function startElement($parser, $name, $attrs) {
global $depth;
global $stack;
global $tree;
$element = array();
foreach ($attrs as $key => $value) {
$element[strtolower($key)]=$value;
}
end($stack);
$stack[key($stack)][strtolower($name)] = &$element;
$stack[strtolower($name)] = &$element;
$depth++;
}
function endElement($parser, $name) {
global $depth;
global $stack;
array_pop($stack);
$depth--;
}
$xml_parser = xml_parser_create();
xml_set_object ( $xml_parser, $this );
xml_set_element_handler($xml_parser, "startElement", "endElement");
//xml_set_character_data_handler ( $parser, "tagContent" );
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, filesize($filename))) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
$tree = end(end($stack));
echo "<pre>";
print_r($tree);
echo "</pre>";
php]
$test = 'This is ';
$test.= 'a';
$test.= ' test';
echo $test
So if you change -
foreach ($attrs as $key => $value) {
//$element[strtolower($key)]=$value;
$element[strtolower($key)][b].=[/b] $value;
}
<edit>
If there is not going to be any content within the tags that you want then you can get rid of the xml_set_character_data_handler. As this works with the contents/data within each tag.
[edited by: PHP_Chimp at 5:23 pm (utc) on Feb. 22, 2008]
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>
<php
$theFile = file_get_contents($CreditResponse);
$newstring=utf8_encode($theFile);
if (!$dom = domxml_open_file($newstring)) {
echo "Error while parsing the document\n";
echo print_r($error);
exit;
}
$root = $dom->document_element();
?>
My if statement always fails and reads the generic error message. I'm not really sure why its not working, yet I echoed out the $newstring and got my entire xml file so it seems to be something with the domxml_open_file command. I just don't know what to check...Any help would be greatly appreciated.
<?php
// $data = file_get_contents($file); // slurp the file into a string
// to test use this
$data = <<<FILE
<xml>
<tag1 attr1="value 1" attr2="value 2" />
<tag2 attr1="value 1" />
<tag1 attr1="value 1 again" attr2=" value 2 again" />
<tag2 attr1="value 1.2 again" />
<tag3 attr1_3="this is different" />
</xml>
FILE;
$xml_parser = xml_parser_create('ISO-8859-1');
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
//xml_set_character_data_handler ( $xml_parser, "tagContent" );
$out = array();// this will store the output
$depth = 0;
function startElement($xml_parser, $tag, $attr) {
global $out, $depth;
if(empty($attr)) {
$out[$depth] = $tag;
$depth++;
}
else { // there are attributes
// non-overwriting
foreach($attr as $key => $value) {
$out[$depth][$tag][strtolower($key)] = $value;
}
$depth++;
}
}
function endElement($xml_parser, $tag) {
global $out;
// do nothing as we are only adding these values to an array.
}
if (!xml_parse($xml_parser, $data /*, feof($fp) */ )) {
die(sprintf("XML error: %s at line %d",xml_error_string(xml_get_error_code($xml_parser)),xml_get_current_line_number($xml_parser)));
}
xml_parser_free($xml_parser);
echo '<pre>';
print_r($out);
echo '</pre>';
echo $data;
?>
Will give you have all of the tags including there attributes listed, none are overwritten.
Finding what you need should then be a matter of looking for the tags that you want.
Hopefully this is what you were after :)