Current location: Hot Scripts Forums » Programming Languages » PHP » Mental XML and attributes

Mental XML and attributes

Reply
  #1 (permalink)  
Old 05-15-09, 04:35 PM
mogant0 mogant0 is offline
Newbie Coder
 
Join Date: May 2009
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Rolleyes Mental XML and attributes

Hiya am a newbie so be gentle please.
I am trying to discover how to update a blooming attribute on an existing xml file????? and its driving me mental.

I have loaded the wee besom into php.
Am using simplexml.
I can read the xml and display the atrribute
. . . I can write a new xml file with the attribute
BUT what I would so dearly love to do is read the xml file and then change the attrib . . . . .and then update the XML.
Is this possible I am beginning to ask myself.

Now normaly I would store the data in mysql but am using flash which is using the xml for data so would like to adjust the xml content.

Heres a wee snippet of the XML

<calendar>
<once>
<date year="2009" month="5" day="13">
<event>I went wobbly</event>
</date>
<date year="2009" month="5" day="14">
<event>I started to cry</event>
</date>
<date year="2009" month="5" day="15">
<event>I went completely mental</event>
</date>
</once>
<</calendar>

So what I would like to do is access the date attrib "year" and then let the user in a form change it to another value.


PLEASE someone HELLLLPPPPPP

Last edited by mogant0; 05-15-09 at 04:55 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-16-09, 04:03 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,572
Thanks: 5
Thanked 27 Times in 24 Posts
PHP Code:
$string '
<calendar>
    <once>
    <date year="2009" month="5" day="13">
    <event>I went wobbly</event>
    </date>
    <date year="2009" month="5" day="14">
    <event>I started to cry</event>
    </date>
    <date year="2009" month="5" day="15">
    <event>I went completely mental</event>
    </date>
    </once>
</calendar>'
;

// Convert to SimpleXML
$xml = new SimpleXMLElement($string);

// Get year
echo $xml->once->date[0]['year'];

// Modify year
$xml->once->date[0]['year'] = '2003';

// Get modified year
echo $xml->once->date[0]['year'];

// Get whole modified XML.
echo $xml->asXML(); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-16-09, 08:30 AM
mogant0 mogant0 is offline
Newbie Coder
 
Join Date: May 2009
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hooraaaahh heres the code

Thanks you are a star . . . .couldnt see the woods for the trees when looking at examples i found
Anyhow thanks again this is the test code i've setup

PHP Code:
<?php
  
// Here am loading up the original calendar xml file
  
$file "calendar.xml"//this will actualy be passed when php is invoced
  
$xml simplexml_load_file($file); //create the simplexml

  // Here am emulating form fields coming in for attributes and event node
  // And changing the contents of these in the simplexml string. Will have
  // identified the occurance of date i wanna change and passed this through too
  // in this case have hard coded the first element ie:[0]
  
$formyear "2009";
  
$formmonth "5";
  
$formday "16";
  
$formevent "The day I did it";
  
$xml->once->date[0]['year'] = $formyear;
  
$xml->once->date[0]['month'] = $formmonth;
  
$xml->once->date[0]['day'] = $formday;
  
$xml->once->date[0]->event $formevent;

  
// convert to DOM so we can rewrite the calendar xml file.
  
$dom_xml dom_import_simplexml($xml); 
  
$dom = new DomDocument();
  
$dom_xml $dom->importNode($dom_xmltrue);
  
$dom_xml $dom->appendChild($dom_xml);
  print 
$dom->save("calendar.xml")
?>
I do be a very happy bunny
mogant
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shopping.com XML Feed Parsing Help angmi90 PHP 4 04-04-05 06:51 PM
Advice parsing a html table to xml RossC0 JavaScript 3 03-22-05 02:10 PM


All times are GMT -5. The time now is 08:46 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.