Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Writing XML to Specific Node with SimpleXML
Old 06-09-2010, 08:41 PM Writing XML to Specific Node with SimpleXML
Average Talker

Posts: 19
Trades: 0
Hey,

So I am currently working on CMS that will automate the process of creating an XML file with data from form inputs. I know how to read and write XML using SimpleXML and the DOM.

My question is...how do you write children to a specified node in a document? For example I have this:

Code:
<?xml version="1.0"?>
<shell>
     <chapter id="01">
          <screen id="01">
               <background>...</background>
          </screen>
          <screen id="02" />
     </chapter>
</shell>
What I want to do is be able to append children to the screen node where the id="02". In addition, I will have to be able to append children to any number of screen nodes. There is not a set number of screen nodes per file. Any help would be awesome!

Last edited by pdpullmn612; 06-09-2010 at 08:43 PM..
pdpullmn612 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-10-2010, 03:39 AM Re: Writing XML to Specific Node with SimpleXML
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Use the xpath function to find matching elements and then adjust the element.

For example:

PHP Code:
<?php
$original_xml 
'<'.'?xml version="1.0"?>
<shell>
     <chapter id="01">
          <screen id="01">
               <background>...</background>
          </screen>
          <screen id="02" />
     </chapter>
</shell>'
;

$xml_obj = new SimpleXMLElement($original_xml);

if ((
$screens $xml_obj->xpath('/shell/chapter/screen[@id=02]')) !== false) {
  
$screens[0]->addChild('background','***');
}


echo 
'<pre>'htmlspecialchars($xml_obj->asXML()), '</pre>';
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-11-2010, 09:44 AM Re: Writing XML to Specific Node with SimpleXML
Average Talker

Posts: 19
Trades: 0
Ok, another question:

I have figured out how to add children to the node containing a specific attribute. However, how would I then add an attribute to a child within that node.

For example, I select a node with a specific attribute as such:

Code:
$snData = $xml->xpath('/shell/chapter/screen[@id=' . $snID . ']');
$snData[0]->addChild('background', $bground);
This works fine. However, I need to then add an attribute to a child within that. I thought I could used something like this:

Code:
$snData = $xml->xpath('/shell/chapter/screen[@id=' . $snID . ']/background');
$snData[0]->addAttribute('type', 'video');
But it does not work and I get error messages. Any suggestions?

Last edited by pdpullmn612; 06-11-2010 at 09:47 AM..
pdpullmn612 is offline
Reply With Quote
View Public Profile
 
Old 06-11-2010, 09:53 AM Re: Writing XML to Specific Node with SimpleXML
Average Talker

Posts: 19
Trades: 0
Never mind, I figured it out. I just had to used double slashes instead of a single slash. The code now looks like this:

Code:
$snData = $xml->xpath('/shell/chapter/screen[@id=' . $snID . ']//background');
$snData[0]->addAttribute('type','video');
pdpullmn612 is offline
Reply With Quote
View Public Profile
 
Old 06-11-2010, 02:31 PM Re: Writing XML to Specific Node with SimpleXML
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Thanks for posting your solution. I tried your error code, though, and did not get an error. What was the reported error and the XML you were working with?

Also, when you add a child, the addChild method returns a reference to the child node, so you could also add the attribute to the node w/o having to call a second xpath:

PHP Code:
<?php
$original_xml 
'<'.'?xml version="1.0"?>
<shell>
     <chapter id="01">
          <screen id="01">
               <background>...</background>
          </screen>
          <screen id="02" />
     </chapter>
</shell>'
;

$xml_obj = new SimpleXMLElement($original_xml);

if ((
$screens $xml_obj->xpath('/shell/chapter/screen[@id=02]')) !== false) {
  
$child_node $screens[0]->addChild('background','***');
  
$child_node->addAttribute('type''video');
}

echo 
'<pre>'htmlspecialchars($xml_obj->asXML()), '</pre>';
?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Writing XML to Specific Node with SimpleXML
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.30679 seconds with 12 queries