Jump to content

Parse XML file


gamefreak13
Go to solution Solved by requinix,

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)?
Edited by gamefreak13
Link to comment
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.

Edited by gamefreak13
Link to comment
Share on other sites

  • Solution

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.