Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem creating XML file

11 views
Skip to first unread message

dand...@gmail.com

unread,
Nov 7, 2012, 7:42:52 AM11/7/12
to
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

Denis McMahon

unread,
Nov 7, 2012, 8:44:54 AM11/7/12
to
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

dand...@gmail.com

unread,
Nov 7, 2012, 11:21:47 AM11/7/12
to
Sorry but I don't know what you mean.

Do I've to change only this?

$docAzione->addChild('documento', $row_files{'fm_nomefile'});

with...?

Thanks

Denis McMahon

unread,
Nov 7, 2012, 12:41:50 PM11/7/12
to
I gave an explanation of the task ("You want to add empty children and
then set the "nome" attributes for each child.") and example of code to
create a xml structure with a root node containing multiple child nodes
with attributes, which seemed to be your requirement.

Perhaps you need to look at the php manual for the functions I used.

http://www.php.net/manual/en/simplexmlelement.construct.php
http://www.php.net/manual/en/simplexmlelement.addchild.php
http://www.php.net/manual/en/simplexmlelement.addattribute.php

(You may want to change the /en/ to your preferred language)

I'm not going to write your code (unless you pay my $$ rate), but I will
point you towards the solution and give examples.

Rgds

Denis McMahon

dand...@gmail.com

unread,
Nov 7, 2012, 5:51:46 PM11/7/12
to
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

Michael Fesser

unread,
Nov 7, 2012, 7:50:00 PM11/7/12
to
.oO(dand...@gmail.com)
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
0 new messages