Jump to content

Parse XML file


gamefreak13

Recommended Posts

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)?
Link to comment
https://forums.phpfreaks.com/topic/278944-parse-xml-file/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/278944-parse-xml-file/#findComment-1434939
Share on other sites

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;
}
Link to comment
https://forums.phpfreaks.com/topic/278944-parse-xml-file/#findComment-1434940
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.