Forum Moderators: coopster

Message Too Old, No Replies

XML attributes don't show

Using PHP to parse XML

         

Hester

3:50 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a script running that successfully parses an XML file and displays the results in HTML. But I cannot get the attributes to display.

Consider this example XML code:


<month value="april">
<date>29th</date>
</month>

At the moment I'm having to wrap the month ("april") in tags, but it would be better to define it as an attribute.

My script defines a function that is handled later by the XML parser:

function startElement($parser, $NAME, $attrs) {
global $startTags;
if ($startTags[$NAME]) print $startTags[$NAME];
}

This works fine but only displays the content between the tags (ie: the data). So I added a line in the function above to try and display the attribute also, but cannot get it to work:

print ($startTags[$attrs]);

Initially I tried just "print $attrs" but it gives me the word "Array". Looking at the PHP manual it states:

Arrays are always converted to the string "Array", so you cannot dump out the contents of an array with echo() or print() to see what is inside them. To view one element, you'd do something like echo $arr['foo'].

This is what I did but then it gives me the error "Warning: Illegal offset type..."

What does this mean and how can I get the attribute to display? (Note: I'm still much of a newbie at PHP and don't understand much about programming.)

grahamstewart

4:08 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



as an aside: thats some weird looking xml schema..

Why is april an attribute of month.. sure april is the month?

And why is <date> part of <month>?
Shouldn't month be part of date?


<date>
<day>29th</day>
<month>April</month>
<year>2003</year>
</date>

Or better still, use integer values and then you can handle multiple languages and calendars.

grahamstewart

4:13 pm on May 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh regarding your actual error...

$attrs is an array.
To access the different attributes use the attribute name (in upper case) to de-reference the array..
e.g.

print $attrs['VALUE'];

should print 'april';

alternatively you could use the foreach statement.

Hester

10:21 am on May 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you so much! That code works great. I wondered about referencing the name of the attribute too.

graham stewart: as an aside: thats some weird looking xml schema..

Why is april an attribute of month.. sure april is the month?

And why is <date> part of <month>?

Shouldn't month be part of date?

I'm not sure I agree, but then XML is new to me so forgive me. The full XML file has several months listed. They are all enclosed by a general tag called "<meetings>". Now it seemed silly to me to have a month tag, then a separate one for the word listing the actual month. Maybe I'm thinking in terms of HTML classes here, but I conceived it would be best to add the name of the month (eg: "April") as an attribute to the month tag. Is this bad XML? Should attributes be served only as numbers?

Secondly, date is clearly part of month, because everything within the month tag refers to that month only. So in my full file I have date, venue, time, and so on. Why should the date come first, then the month?

Is XML not flexible to be made up as required? So long as it is "well-formed"?

Here is a sample of my code as it stands: (sorry, the forum has stripped out the tabs at the start of each line)


<?xml version="1.0" encoding="UTF-8"?>
<meetings>
<month>
<name>April 2003</name>
<meeting>
<type>Advanced Spells</type>
<agenda/>
<agenda_text/>
<venue>University of Magic</venue>
<day>Tuesday</day>
<date>29th</date>
<time>10:30 am</time>
<room/>
<map_link>http://www[snip]</map_link>
<map_text>Directions</map_text>
</meeting>
</month>
</meetings>

I hope to improve it by shortening the number of tags and introducing attributes, hence this thread.

grahamstewart

1:04 pm on May 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is XML not flexible to be made up as required? So long as it is "well-formed"?

Well.. yes and no.
XML should be written so that the structure of the XML reflects the logical structure of the data.

So a date contains a day, a month and a year. A meeting has a location, a name, a date and an agenda. And an agenda consists of multiple items.

With that in mind, heres a possible version of that schema that you might want to try...

[pre]
<?xml version="1.0" encoding="UTF-8"?>
<meeting>
<type>Advanced Spells</type>
<location>
<building>Hogwarts</building>
<room>5a</room>
<map_link>http://www....</map_link>
</location>
<date>
<day>29</day>
<month>4</month>
<year>2003</year>
</date>
<agenda>
<item>Frogs to Princes</item>
<item>Princes to Frogs</item>
</agenda>
</meeting>
[/pre]

See what I mean?

Hester

1:46 pm on May 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<date> 
<day>29</day>
<month>4</month>
<year>2003</year>
</date>

While that may be more 'logically' correct, it introduces much more code bloat. Many date formats are possible:

<date>29th April 2003</date>

<date>29/04/03</date>

<date>Tuesday</date>

Since I'm converting directly to HTML, it's easier to have the data almost the same in the XML as it will appear on screen. Using your example above, I'd have to convert the month to a word (4 => "April"), work out the suffix of the day (eg: "23" => "23rd", whilst "14" => "14th") all adding to the programming.

I see what you mean about 'date' defining 'month' and so on. It's just the way I chose to write it when I did "My First XML File" TM. :-)

I'll get better at it!

grahamstewart

2:16 pm on May 2, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it introduces much more code bloat

Yup, thats XML for you. The more flexible you make it the more bloated it gets. The formats you suggest only make sense in your culture, they would cause problems when exchanging data internationally between different systems (which is the general idea behind XML).

I'd have to convert the month to a word (4 => "April"), work out the suffix of the day (eg: "23" => "23rd", whilst "14" => "14th") all adding to the programming.

Thats what XSL [w3.org] is for.