Question : Parsing an XML string in PHP

I am making a query to a database service that is returning the data in an XML string. The following is an example of what is being returned. I have no idea how to go about parsing this into useable data. This XML appears to be much more complicated than all of the examples I have been able to find and my attempts at parsing this have failed. I only need the data from the <record id=..." tag on. I also need to retrieve the value that is set in the record tag. Thanks for your assistance.

<?xml version="1.0" encoding="utf-8"?>
<ieiresponse>
  <requestinformation code="100">
    <codemessage>RECORDS FOUND</codemessage>
    <inputs>
      <firstname>RANDY</firstname>
      <lastname>LAWRENCE</lastname>
      <product>OFFENDERSCAN</product>
      <includenulldob>0</includenulldob>
      <includesexoffendernulldobs>1</includesexoffendernulldobs>
      <exactdobmatchonly>0</exactdobmatchonly>
      <exactnamematchonly>0</exactnamematchonly>
      <probability>DEFAULT</probability>
      <includejurisdictionssearched>0</includejurisdictionssearched>
    </inputs>
  </requestinformation>
  <criminalinformation>
    <records count="4">
      <record id="owUn9D/HerIRpF6gI2EsFw==">
        <category>SEX OFFENDER</category>
        <sourceorjurisdiction>NJ SEX OFFENDER REGISTRY</sourceorjurisdiction>
        <fullname>RANDY S LAWRENCE</fullname>
        <firstname>RANDY</firstname>
        <middlename>S</middlename>
        <lastname>LAWRENCE</lastname>
        <fulldob>09/21/1967</fulldob>
        <sex>MALE</sex>
        <race>BLACK</race>
        <haircolor>BLACK</haircolor>
        <eyecolor>BROWN</eyecolor>
        <weight>215 LBS</weight>
        <height>5 FEET 09 INCHES</height>
        <scarsmarks>TATTOO:LEFT ARM - MP/NEW YORK; TATTOO:LEFT FOREARM - CHINESE SYMBOL; TATTOO:LEFT FOREARM - JAPAN WRITING; TATTOO:NECK - BRAVE HEART/SHARK; TATTOO:RIGHT ARM - BAND W/MOON &amp; STAR</scarsmarks>
        <age>42</age>
        <photoname>https://xml.innovativedatasolutions.com/NatCrimWs/getPhoto.aspx?photo=g4iXOnXl7uaRiZYvdAhgMdPFS4WIF36dJ2pE6s5NSs4%3d</photoname>
        <comments>DATE PUBLISHED:  03/18/2008; ALIAS DATE OF BIRTH/AGE:  01/02/1965, 45, 09/12/1967, 42; PHOTO DATE:  07/10/2009;OFFENDER RISK LEVEL: MODERATE</comments>
        <aliases count="10">
          <alias>
            <fullname>RAHIB LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>RANDAL LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>RANDAL S LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>RANDALL LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>RANDALL S LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>RANDY LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>VERNON LAWRENCE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>LAWRENCE RANDAL</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>LAWRENCE S RANDAL</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
          <alias>
            <fullname>KNIGHT S SUGE</fullname>
            <fulldob>09/21/1967</fulldob>
          </alias>
        </aliases>
        <addresses count="1">
          <address>
            <address1>591 BUCKELEW AVENUE</address1>
            <city>MONROE TWP</city>
            <state>NJ</state>
            <zip>08831</zip>
          </address>
        </addresses>
        <offenses count="1">
          <offense>
            <offensedescription>SEXUAL ASSAULT</offensedescription>
            <convictiondate>03/04/2002</convictiondate>
            <convictionlocation>MIDDLESEX</convictionlocation>
            <comments>EVENT DETAILS:  OFFENDER HAD CONSENSUAL SEX WITH A JUVENILE FEMALE ACQUAINTANCE.;VICTIM AGE: UNDER18;  VICTIM GENDER: FEMALE</comments>
          </offense>
        </offenses>
      </record>
      <record id="4B27YEI8aFq0da+nqlT2rg==">
        <category>SEX OFFENDER</category>
        <sourceorjurisdiction>FL SEX OFFENDER REGISTRY</sourceorjurisdiction>
        <fullname>RANDY THOMAS LAWRENCE</fullname>
        <firstname>RANDY</firstname>
        <middlename>THOMAS</middlename>
        <lastname>LAWRENCE</lastname>
        <fulldob>06/10/1955</fulldob>
        <sex>MALE</sex>
        <race>WHITE</race>
        <haircolor>BLACK</haircolor>
        <eyecolor>HAZEL</eyecolor>
        <weight>115 LBS</weight>
        <height>5 FEET 04 INCHES</height>
        <photoname>https://xml.innovativedatasolutions.com/NatCrimWs/getPhoto.aspx?photo=7cvZtkfGuoMQAy%2fb%2b%2bbolXN5GkDmywnvprG3pkij8uJuPC15hkQHzJ8EH4OP%2fkbr</photoname>
        <comments>STATUS:  SUPERVISED - FL DEPT OF CORRECTIONS;ID NUMBERS: DEPARTMENT OF CORRECTIONS NUMBER:K66666;  OFFENDER CLASSIFICATION: OFFENDER</comments>
        <addresses count="1">
          <address>
            <address1>1218 ORANGE AVE</address1>
            <city>FORT PIERCE</city>
            <state>FL</state>
            <zip>34950-8858</zip>
          </address>
        </addresses>
        <offenses count="1">
          <offense>
            <comments>VICTIM AGE: ADULT</comments>
          </offense>
        </offenses>
      </record>
 

Answer : Parsing an XML string in PHP

Here you go, assuming you have the XML in a string then the following should illustrate what you need to do to extract it.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
<?php

$data = ..... load your data


$xml = simplexml_load_string( $data );

$firstName = (string) $xml->requestinformation->inputs->firstname;
$lastName = (string) $xml->requestinformation->inputs->lastname;


echo "$firstName $lastName<br/>";

foreach( $xml->criminalinformation->records->record as $aRecord ) {
     $category = (string)$aRecord->category;
     $fullName = (string)$aRecord->fullname;

     echo "Offender: $fullName - $category<br/>";

     foreach ( $aRecord->aliases->alias as $anAlias )
         echo "{$anAlias->fullname}<br/>";
}
Random Solutions  
 
programming4us programming4us