473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to list XML element attributes?

abdoelmasry
104 100+
Hi Friends

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
  1. <xml version="1.0" encoding="windows-1256"?>
  2. <books>
  3. <book id="1" name="PHP Book"></book>
  4. <book id="2" name="C# Book"></book> 
  5. <book id="3" name="PatchScripts"></book> 
  6. <book id="4" name="Assemply"></book> 
  7. </books>
  8.  
i wrote this PHP Code to read "books.xml" file:
File Name: xml_reader.php
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. $xml_hwnd=new domDocument();
  4. $xml_hwnd->Load("books.xml");
  5. $books=$xml_hwnd->getElementsByTagName("book");
  6. $books_array=array();
  7. //Loop to get Elements Contents
  8. foreach($books as $book){
  9.     $bokid=$book->getAttribute("id");
  10.     $bokname=$book->getAttribute("name");
  11. $books_array[$bokid]=array("id"=>$bokid,"name"=>$bokname);
  12. }
  13. // Print Books List
  14. print_r($books_array);
  15. ?>
  16.  
the previous code assumes that i know each attribute name,
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
  1. $book->getAttribute("id");
  2. $book->getAttribute("name");
  3. $book->getAttribute("author");
  4. $book->getAttribute("etc ...");
  5.  
i need to list all attributes related to any element without mention every element name.

Thanks
Feb 6 '11 #1
2 3856
Dormilich
8,658 Expert Mod 8TB
you can get a list of attributes by $attlist = $book->attributes;
Feb 6 '11 #2
abdoelmasry
104 100+
Thank you very much Dormilich

I did it by the following code:

Expand|Select|Wrap|Line Numbers
  1. $attlist = $book->attributes;
  2. foreach($attlist as $attritem){
  3. echo "Attribute Name: ".$attritem->name;
  4. echo "Attribute Value: ".$attritem->value;
  5. }
  6.  
:)
Feb 7 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: Haines Brown | last post by:
I have a table with three columns, and I want the data in the first column to align left, while that in the remaining columns to align right: #testTable { text-align: right; } #leftcol {...
1
by: Wayne Lian via .NET 247 | last post by:
Hi all, Just wonder anyone have encountered this problem before?I tried using XMLSPY debugger and I can get the correct outputfor my XSLT transformation, however in .net, aftertransformation the...
1
by: serge calderara | last post by:
Dear all, where is the place to get the list of each valid attributes for individual Element of a confi file or Html page view? thanks fr your help regards serge
4
by: joewhitehair | last post by:
Using a schema, I created classes for my web service using the XSD.exe tool. With the classes I then wrote a web service function, but the WSDL that is generated has different minOccurs and...
2
by: Mikus Sleiners | last post by:
I want to create xml document that looks like this: <?xml version="1.0" encoding="utf-16"?> <Draft xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"...
7
by: Rehceb Rotkiv | last post by:
I want to check whether, for example, the element myList exists. So far I did it like this: index = -3 if len(myList) >= abs(index): print myList Another idea I had was to (ab-?)use the...
2
by: Thomas Guettler | last post by:
Hi, how can you list the attributes of an object if you catch an AttributeError? I couldn't find a reference in the exception object, which points to the object. I want to call dir() on...
5
by: antar2 | last post by:
Hello, I am a beginner in Python and am not able to use a list element for regular expression, substitutions. list1 = list2 = Suppose that I want to substitute the vowels from list2 that...
2
by: cloftis | last post by:
Using VS2003, VB and MSHTML, Using an HTMLSpanElement I want to enumerate the attributes of a SPAN tag. 1 'For testing sake 2 Dim strMarkup as String = "<span attr1='somevalue'...
1
by: libish | last post by:
hi all, can any one suggest me on selcting a list element? i am displaying a list containing some items dynamically... after displaying the content and the form holding this list, i need to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.