i need help to read XML file to an Array using PHP
i have this code xml file:
File name: books.xml
Expand|Select|Wrap|Line Numbers
- <xml version="1.0" encoding="windows-1256"?>
- <books>
- <book id="1" name="PHP Book"></book>
- <book id="2" name="C# Book"></book>
- <book id="3" name="PatchScripts"></book>
- <book id="4" name="Assemply"></book>
- </books>
File Name: xml_reader.php
Expand|Select|Wrap|Line Numbers
- <?php
- $xml_hwnd=new domDocument();
- $xml_hwnd->Load("books.xml");
- $books=$xml_hwnd->getElementsByTagName("book");
- $books_array=array();
- //Loop to get Elements Contents
- foreach($books as $book){
- $bokid=$book->getAttribute("id");
- $bokname=$book->getAttribute("name");
- $books_array[$bokid]=array("id"=>$bokid,"name"=>$bokname);
- }
- // Print Books List
- print_r($books_array);
- ?>
what about if i don't know attribute name ??
or i have many elements and it's too hard to call every attribute using it's name just like:
Expand|Select|Wrap|Line Numbers
- $book->getAttribute("id");
- $book->getAttribute("name");
- $book->getAttribute("author");
- $book->getAttribute("etc ...");
Thanks