SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
Thread: XML Arrays using PHP
-
Jul 17, 2006, 12:23 #1
- Join Date
- Jan 2005
- Posts
- 147
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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 & 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 & Pre- Schools" /> </Project> </ROOT>
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];
}
Thanks!
-
Jul 17, 2006, 12:30 #2
- Join Date
- Aug 2000
- Location
- Philadephia, PA
- Posts
- 20,578
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
Try Improvely, your online marketing dashboard.
→ Conversion tracking, click fraud detection, A/B testing and more
-
Jul 17, 2006, 12:41 #3
- 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