Trying to pull data from xml file

Discussion in 'PHP' started by mthacker, Feb 13, 2008.

  1. #1
    I'm trying to pull and save data from an xml file that I'm receiving after sending a post request to a remote server. The issue is the layout of the tags, and that I don't have much php knowledge. The tags look like the following:

    <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 ?
     
    mthacker, Feb 13, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    If you don't have much PHP knowledge, you may want to try a regular expression:

    
    <?php
    
    // The XML Data
    $data = <<<eof
    <RESPONDING_PARTY _Name="The Co name" _StreetAddress="The Address" _City="The City" _State="The State" _PostalCode="The ZIP">
    eof;
    
    // Perform Regular Expression
    preg_match("`<RESPONDING_PARTY _Name=\"(.*?)\" _StreetAddress=\"(.*?)\" _City=\"(.*?)\" _State=\"(.*?)\" _PostalCode=\"(.*?)\"`i", $data, $matches);
    
    // The first element is useless for this example
    unset($matches[0]);
    
    // Output eaech $match
    print_r($matches);
    
    // How to call one from that array (use $matches[item_number])
    echo "ZIP Code: $matches[5]";
    ?>
    
    PHP:
    Output:

    
    Array
    (
        [1] => The Co name
        [2] => The Address
        [3] => The City
        [4] => The State
        [5] => The ZIP
    )
    Zip Code: The ZIP
    
    Code (markup):
    Hope this helps,

    Jay
     
    jayshah, Feb 13, 2008 IP
  3. mthacker

    mthacker Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes that would work, but our xml file is not a consistant size and also vasries on the number of tags that may be present and multiples of several tags can appear throughout the file. I really need to figure out the xml parsing in order for this to work but thats looking pretty difficult to figure out. Any suggestions
     
    mthacker, Feb 15, 2008 IP
  4. walkere

    walkere Active Member

    Messages:
    112
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Building your own XML parser can be pretty complicated, yes. Using the built-in SimpleXML parser is pretty... simple.

    You might want to check out the php.net documentation and the relevant section of Practical PHP Programming. You could also check the PHP Tutorials in my sig - there's an example of using SimpleXML to read/parse an rss feed.

    I originally built a simple parser to work with XML, and I found it pretty difficult for anything but very simple stuff. Once I figured out how to use SimpleXML, tho, I found it a breeze to work with xml files.

    Good luck,
    - Walkere
     
    walkere, Feb 15, 2008 IP
  5. blacknet

    blacknet Active Member

    Messages:
    709
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    70
    #5
    blacknet, Feb 15, 2008 IP
  6. mthacker

    mthacker Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    These do look like good option but unfotunately I'm working on PHP 4.4.4 and both of those options are for php5
     
    mthacker, Feb 15, 2008 IP
  7. blacknet

    blacknet Active Member

    Messages:
    709
    Likes Received:
    16
    Best Answers:
    2
    Trophy Points:
    70
    #7
    blacknet, Feb 15, 2008 IP
  8. mthacker

    mthacker Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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?
     
    mthacker, Feb 22, 2008 IP