FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Problem creating XML file
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Problem creating XML file [message #179556] Wed, 07 November 2012 07:42 Go to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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 08:44 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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 #179558 is a reply to message #179557] Wed, 07 November 2012 11:21 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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
Re: Problem creating XML file [message #179560 is a reply to message #179558] Wed, 07 November 2012 12:41 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On Wed, 07 Nov 2012 08:21:47 -0800, dandi.box wrote:

> Sorry but I don't know what you mean.
>
> Do I've to change only this?
>
> $docAzione->addChild('documento', $row_files{'fm_nomefile'});
>
> with...?

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
Re: Problem creating XML file [message #179565 is a reply to message #179560] Wed, 07 November 2012 17:51 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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] Wed, 07 November 2012 19:50 Go to previous message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
.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
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: How awesome Virtual Sailor 7 is
Next Topic: Order of Echoed HTML Output
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Dec 18 09:53:08 EST 2017

Total time taken to generate the page: 0.00742 seconds