SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy XML Arrays using PHP

    What i'm trying to do is ultimately insert some data from an xml file into a mysql database. If i could get the xml data into an array with all attributes i can take it form there and do my thing.

    I've been working with some code that i pulled directly from the php website (users comments) and it skips an element holding the most critical data. Here is the data and the php code that i'm trying to work with.

    The xml file. (cutt down to make it easier but the guts are the same for every project entry)
    Code:
    <ROOT>
    <Project ProjectID="11023" ProjectTitle="PETTUS ROAD &amp; PINE GROVE ROAD SCHOOL" Value="11000000.0000" StageComments1="Project Canceled" UpdateDate="01/05/2006" BidDate="06/30/2005" BidTime="02:00 PM" Stage="Project Cancelled" PlansSpecsURL="PlansSpecs.aspx">
      <UpdateSummary UpdateSummary="1" />
      <Details ConnectUser="snewman" FlaggedForExport="Yes" />
      <Architect CompanyName="SKT ARCHITECTS PC" CompanyURL="Company.aspx" CompanyContactName="Mr. Robert VanPeursem" ContactNameURL="Contact.aspx" />
      <Owner CompanyName="MADISON CO BOARD OF EDUCATION PURCHASING" CompanyURL="Company.aspx" CompanyContactName="Mr. Kerry H Wilkerson" ContactNameURL="Contact.aspx" />
      <ProjectCategory CategoryDescription="Elementary &amp; Pre- Schools" />
    </Project>
    </ROOT>
    Here is the php code that i'm working with.

    PHP Code:
    $xml "ProjectExport.xml";

    $xmlary = array ();

    if ((
    strlen ($xml) < 24576) && is_file ($xml))
        
    $xml file_get_contents ($xml);
        
        
    $ReElements '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<(\/\s*\1\s*)>)/s';
        
    $ReAttributes '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
        
    preg_match_all ($ReElements$xml$elements);   
        
        foreach (
    $elements[1] as $ie => $xx) {
            
    $xmlary[$ie]["name"] = $elements[1][$ie];
        if ( 
    $attributes trim($elements[2][$ie])) {
          
    preg_match_all ($ReAttributes$attributes$att);
          
          foreach (
    $att[1] as $ia => $xx)
          
    // all the attributes for current element are added here
            
    $xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
        } 
    // if $attributes
        
        // get text if it's combined with sub elements
        
    $cdend strpos($elements[3][$ie],"<");
        
        if (
    $cdend 0) {
          
    $xmlary[$ie]["text"] = substr($elements[3][$ie],0,$cdend -1);
        } 
    // if cdend
        
        
    if (preg_match ($ReElements$elements[3][$ie])){       
            
    $xmlary[$ie]["elements"] = xml2array ($elements[3][$ie]);
        }else if (
    $elements[3][$ie]){
          
    $xmlary[$ie]["text"] = $elements[3][$ie];
        }
     
    $xmlary[$ie]["closetag"] = $elements[4][$ie];

    Now everything appears to work fine at first. I get an array holding a bunch of data. Everything is there.. except the Project element and all of its attributes. Thats the most important peace of the file. I need to be able to pull all elements and attributes. It pulls everything perfectly except the project element. Any ideas why is skips this element? or how i might be able to get this element in the array along with its attributes?

    Thanks!

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply but that function when called gives an empty array. any idea why its not working or maybe why the one i'm currently using is grabbing the first element?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •