Connecting Tech Pros Worldwide Forums | Help | Site Map

DOMXML get unknown Attributes

Nikolai Onken
Guest
 
Posts: n/a
#1: Jul 17 '05
Hey List,

I was wondering how I can get all Attributes of a XML-Tag without
knowing them. The XML could look like following:

<plugin>
<args level="1" depth="2"/>
</plugin>
<plugin>
<args permission="23"/>
</plugin>

Now both plugins use different Attributes in the args tags and the
program doesn't know them. Is there a way to get them? Put them into
an array? I am using PHP5
Thanks a lot and best regards,

Nikolai Onken

Janwillem Borleffs
Guest
 
Posts: n/a
#2: Jul 17 '05

re: DOMXML get unknown Attributes


Nikolai Onken wrote:[color=blue]
> Now both plugins use different Attributes in the args tags and the
> program doesn't know them. Is there a way to get them? Put them into
> an array? I am using PHP5
> Thanks a lot and best regards,
>[/color]

Since the DOM function DocumentElement::getAttributes() doesn't seem to be
implemented, you could, per example, use the simplexml extension instead:

<?php

$xml = <<<EOXML
<plugins>
<plugin>
<args level="1" depth="2"/>
</plugin>
<plugin>
<args permission="23"/>
</plugin>
</plugins>
EOXML;

$dom = new DomDocument;
$dom->loadXML($xml);

$s = simplexml_import_dom($dom);

foreach ($s->plugin as $plugin) {
foreach($plugin->args[0]->attributes() as $n => $v) {
print "$n => $v<br />";
}
}

?>


HTH;
JW



Simon Stienen
Guest
 
Posts: n/a
#3: Jul 17 '05

re: DOMXML get unknown Attributes


Nikolai Onken <info@nikolaionken.com> wrote:[color=blue]
> Hey List,
>
> I was wondering how I can get all Attributes of a XML-Tag without
> knowing them. The XML could look like following:
>
> <plugin>
> <args level="1" depth="2"/>
> </plugin>
> <plugin>
> <args permission="23"/>
> </plugin>
>
> Now both plugins use different Attributes in the args tags and the
> program doesn't know them. Is there a way to get them? Put them into
> an array? I am using PHP5
> Thanks a lot and best regards,
>
> Nikolai Onken[/color]

Since the XML-document should follow a specific DTD, you should know
*every* possible attribute. If you are just beginning to create such a
structure, maybe you can change it to something like:

<plugin>
<arg name="level" value="1" />
<arg name="depth" value="2" />
</plugin>
<plugin>
<arg name="permission" value="23" />
</plugin>


--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Nikolai Onken
Guest
 
Posts: n/a
#4: Jul 17 '05

re: DOMXML get unknown Attributes


On Thu, 23 Sep 2004 08:57:50 +0200, Simon Stienen
<simon.stienen@news.slashlife.de> wrote:
[color=blue]
><plugin>
> <arg name="level" value="1" />
> <arg name="depth" value="2" />
></plugin>
><plugin>
> <arg name="permission" value="23" />
></plugin>[/color]

Hey Simon,

I was thinking about that and its true - otherwise the idea of XML
wouldn't really be followed... I'll use the name="" value="" instead.
Thanks for your help!
Regards,

Nikolai
Closed Thread