Problem creating XML file [message #179556] |
Wed, 07 November 2012 12:42 |
Sarah
Messages: 30 Registered: December 2010
Karma: 0
|
Member |
|
|
I need to create this structure
<docAzione>
<documento nome="Prova1.txt" />
<documento nome="Prova2.txt" />
</docAzione>
I tried doing:
$docAzione = $azione->addChild('docAzione');
while ($row_files = mysql_fetch_array($result_files,MYSQL_ASSOC)) {
......
$docAzione->addChild('documento', $row_files{'fm_nomefile'});
But result is:
<docAzione>
<documento>Prova1.txt</documento>
<documento>Prova2.txt</documento>
</docAzione>
(wrong)
So I tried with
$docAzione->addAttribute('nome', $row_files{'fm_nomefile'});
And I show:
<docAzione nome="Prova1.txt"/>
(wrong)
Where is it my error?
Thanks
|
|
|
Re: Problem creating XML file [message #179557 is a reply to message #179556] |
Wed, 07 November 2012 13:44 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Wed, 07 Nov 2012 04:42:52 -0800, dandi.box wrote:
> I need to create this structure
>
> <docAzione>
> <documento nome="Prova1.txt" />
> <documento nome="Prova2.txt" />
> </docAzione>
>
> I tried doing:
>
> $docAzione = $azione->addChild('docAzione');
>
> while ($row_files = mysql_fetch_array($result_files,MYSQL_ASSOC)) {
>
> .....
>
> $docAzione->addChild('documento', $row_files{'fm_nomefile'});
>
>
> But result is:
>
> <docAzione>
> <documento>Prova1.txt</documento> <documento>Prova2.txt</documento>
> </docAzione>
>
> (wrong)
>
> So I tried with
>
> $docAzione->addAttribute('nome', $row_files{'fm_nomefile'});
>
> And I show:
>
> <docAzione nome="Prova1.txt"/>
>
> (wrong)
You want to add empty children and then set the "nome" attributes for
each child.
So maybe something like this:
<?php
$list = array( "jim", "susan", "henry" );
$p = new SimpleXMLElement( '<people></people>' );
foreach ( $list as $k => $d ) {
$c = $p->addChild( 'person' );
$c->addAttribute( 'name', $d );
}
echo $p->asXML();
?>
Output:
<?xml version="1.0"?>
<people><person name="jim"/><person name="susan"/><person name="henry"/></
people>
Rgds
Denis McMahon
|
|
|
|
|
Re: Problem creating XML file [message #179565 is a reply to message #179560] |
Wed, 07 November 2012 22:51 |
Sarah
Messages: 30 Registered: December 2010
Karma: 0
|
Member |
|
|
Why are you talking about to create an empty children?
I need this:
<docAzione>
<documento nome="Prova1.txt" />
<documento nome="Prova2.txt" />
</docAzione>
Why do you see "empty children"?
Thanks
|
|
|
Re: Problem creating XML file [message #179567 is a reply to message #179565] |
Thu, 08 November 2012 00:50 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
.oO(dandi(dot)box(at)gmail(dot)com)
> Why are you talking about to create an empty children?
>
> I need this:
>
> <docAzione>
> <documento nome="Prova1.txt" />
> <documento nome="Prova2.txt" />
> </docAzione>
>
> Why do you see "empty children"?
Because your <documento/> are empty children, they don't have any
content. 'nome' is an attribute, but the elements themselves are empty.
This is an empty element:
<foo/> or <foo></foo>
This is a non-empty element:
<foo>bar</foo>
So, for each row from your database, create an empty 'documento'
element, add a 'nome' attribute to it with the value from the DB and
then append the whole thing to the root 'docAzione' element.
Micha
--
http://mfesser.de/
Fotos | Blog | Flohmarkt
|
|
|