0

I am using following code to parse the following xml and add the id of each person as key of the array and their names as the values of the array.

The code properly works but the array is not.

   $array = array();
   $category = $xml->xpath('descendant::person');
    foreach ($person as $p) {
    $array[$p['id']] = $p['name'];
    }
 <?xml version="1.0" encoding="utf-8"?>
 <people>
   <person name="Joe" id="134">
   <person name="Jack" id="267">
   </person>
   </person>

   <person name="Ray" id="388">
   <person name="John" id="485">
   <person name="Rayan" id="900">
   </person>
   </person>

   <person name="Alex" id="590">
   </person>
 </people>

The XML is not valid but I ca not make it valid. However the code is working and I just need to assign the ids and values to the array.

hakre
  • 193,403
  • 52
  • 435
  • 836
Saeed Pirdost
  • 167
  • 3
  • 12

3 Answers3

1

I don't know this is the correct method, But I have tested this one and its working fine,

$xml = simplexml_load_string($response);
 $category = $xml->xpath('descendant::person');

 $array = array();

  foreach($category as $each){
    $name_obj = $each->attributes()->name[0];

     $name_json = json_encode($name_obj);
    $name_array = json_decode($name_json, TRUE);


    $id_obj = $each->attributes()->id[0];

  $id_json = json_encode($id_obj);
  $id_array = json_decode($id_json, TRUE);

  $array[$id_array[0]] = $name_array[0];
}

 print_r($array);
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
1

Lots of little issues going on here ... the biggest problem, though, is that you can't use a simplexml object node as an index in an array. It has to be manually cast as a string or integer. You'd also be better served tweaking your xpath expression a bit, and your loop shouldn't be on $person, which is a variable that doesn't exist, but instead on $category. Try this as an alternative:

$array = array();
$category = $xml->xpath('//person');
while(list( , $p) = each($category)) {
        $array[(string)$p['id']] = (string)$p['name'];
}
print_r($array);

Also note that, if your XML is not valid XML, then it does matter ... simplexml libraries will never function properly on invalid XML (the XML in your example has some improper nesting).

jlmcdonald
  • 13,408
  • 2
  • 54
  • 64
  • thanks for the answer, when I added the casting it worked but when changed it to //person does not – Saeed Pirdost Dec 28 '12 at 06:11
  • is there any other library that I can use to parse this invalid xml ? – Saeed Pirdost Dec 28 '12 at 06:14
  • Even though you don't have control of the XML you get, you can always tidy it up first .. with something like HTMLtidy. It'll at least get it into a format where an XML parser will run on it. Another possibility is to use the DOM library, and its loadHTML() method ... it doesn't care about malformed XML, and you can then run your XPath on it. – jlmcdonald Dec 28 '12 at 06:23
  • great thanks do you have any sample or tutorial of either of them? there are lots of information I am not sure which one to choose – Saeed Pirdost Dec 28 '12 at 06:54
  • could try this question: http://stackoverflow.com/questions/292926/robust-mature-html-parser-for-php -- it targets HTML processing but the methodologies should work for you just fine as well. – jlmcdonald Dec 28 '12 at 07:08
-1

Are you sure the XML should not be like this?:

<?xml version="1.0" encoding="utf-8"?>
<people>
    <person name="Joe" id="1"></person>
    <person name="Jack" id="2"></person>
    <person name="Ray" id="3"></person>
    <person name="John" id="4"></person>
    <person name="Alex" id="5"></person>
</people>
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
  • I am getting it from a web service, so I cant change it, however the code successfully works and the only thing that I need is to set the id of the persons as the index of array and their names as values of array. moreover id of people are not in order. – Saeed Pirdost Dec 28 '12 at 05:50
  • Hi the id are not in the order so can you fetch the data order by id then id will be in the correct order or sort the array $person just before foreach – Sanjay Dec 28 '12 at 05:53
  • does that make any change in setting the array indexes? I have updated the question – Saeed Pirdost Dec 28 '12 at 05:55
  • Now your XML does not validate (in your original post)... – Sverri M. Olsen Dec 28 '12 at 05:56
  • Does not matter, because my code works, I just need to use the ids as keys of the array etc. – Saeed Pirdost Dec 28 '12 at 05:57
  • If your code works then why are you asking for help? XML that does not validate will throw errors, so it does matter. – Sverri M. Olsen Dec 28 '12 at 06:12