gamefreak13 Posted June 8, 2013 Share Posted June 8, 2013 (edited) I need to parse a very large XML file. About 80% of the XML file is irrelivant, and I only need data inside a certain route. Below is a short example. I need data within State>Center (where ID="INHB")>Dispatch (where ID="BCCC")>. <State> <Center ID="SAHB">...</Center> <Center ID="GGHB">...</Center> <Center ID="LAHB">...</Center> <Center ID="INHB"> <Dispatch ID="INCC">...</Dispatch> <Dispatch ID="OCCC">...</Dispatch> <Dispatch ID="BSCC">...</Dispatch> <Dispatch ID="ICCC">...</Dispatch> <Dispatch ID="BCCC"> <Log ID="130608BC00896"> <LogTime>"Jun 8 2013 4:06PM"</LogTime> <LogType>"Log Type Here"</LogType> <Location>"Location Here"</Location> <LocationDesc>"A Description Of The Location Here"</LocationDesc> <Area>"City Here"</Area> <ThomasBrothers>""</ThomasBrothers> <LATLON>"33434943:117141729"</LATLON> <LogDetails> <details> <DetailTime>"Jun 8 2013 4:06PM"</DetailTime> <IncidentDetail>"line 1"</IncidentDetail> <IncidentDetail>"line 2"</IncidentDetail> <IncidentDetail>"line 3"</IncidentDetail> </details> </LogDetails> </Log> <Log ID="130608BC00879"> etc etc etc </Log> </Dispatch> <Dispatch ID="BICC">...</Dispatch> <Dispatch ID="ECCC">...</Dispatch> <Dispatch ID="CTCC">...</Dispatch> </Center> </State> What I would really love to end up with is a variable for every portion of information. For example: $LogID $LogTime $LogType $Location $LocationDesc $Area $ThomasBrothers $LATLON $DetailTime $IncidentDetail[] (PHP array for each line) All of which are in a foreach loop for each $LogID. My problem is traversing the structure. Can anyone help me get started with some basic code to build upon (specifically in relation to the structure I've presented)? Edited June 8, 2013 by gamefreak13 Quote Link to comment https://forums.phpfreaks.com/topic/278944-parse-xml-file/ Share on other sites More sharing options...
requinix Posted June 9, 2013 Share Posted June 9, 2013 "Very large" as in it can't be loaded into memory with, say, SimpleXML? Or lacking that DOMDocument? Quote Link to comment https://forums.phpfreaks.com/topic/278944-parse-xml-file/#findComment-1434918 Share on other sites More sharing options...
gamefreak13 Posted June 9, 2013 Author Share Posted June 9, 2013 (edited) SimpleXML should have no problem with it. There are 5900 lines (but it can fluctuate by a few thousand) of XML. I think SimpleXML is how I'll need to go, but my problem is the structure of the data, I don't know how to specify the path/locations of the data I want to get. It should be noted that this PHP script will be ran about every 30-60 seconds for the rest of forever to fetch the XML file. This script will (as I build upon it) retrieve the small portion of the XML I need, and insert it to a MySQL database. I know how to do the rest, I just don't know how to get the data I need from the XML. Edited June 9, 2013 by gamefreak13 Quote Link to comment https://forums.phpfreaks.com/topic/278944-parse-xml-file/#findComment-1434939 Share on other sites More sharing options...
Solution requinix Posted June 9, 2013 Solution Share Posted June 9, 2013 If you can get it into SimpleXML then navigating to the data you want is easy. $dispatch = current($xml->xpath("//State/Center[@ID='INHB']/Dispatch[@ID='BCCCC']")); // assuming there's only one match foreach ($dispatch->Log as $log) { // attributes work like $LogID = (string)$log["ID"]; // elements work like $LogTime = (string)$log->LogTime; } Quote Link to comment https://forums.phpfreaks.com/topic/278944-parse-xml-file/#findComment-1434940 Share on other sites More sharing options...
gamefreak13 Posted June 9, 2013 Author Share Posted June 9, 2013 Thank you VERY much! It's that xpath part that I couldn't figure out. I've managed to get past that major hurdle thanks to you and am now flying by at light speed with my project, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/278944-parse-xml-file/#findComment-1434946 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.