473,761 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

>> Very Simple PHP DOM Question..Pleas e help!

Hello everyone,

I am really having a lot of trouble understanding the DOM and I doing
something quite basic. I need to the DOM (in PHP 5) to modify an XML
file. For example the XML basically looks like this.

<news>
<pageContent>
<heading></heading>
<author></author>
</pageContent>
<commentSection >
</commentSection>
</news>

What I am trying to do is simple add nodes and content within
<commentSection > and </commentSection> tags. I have been trying the
DOM PHP 5 functions, but I cannot get it quite right. What are the
functions and order would I use if I wanted to take the above XML file
and modify it to look like this:

<news>
<pageContent>
<heading></heading>
<author></author>
</pageContent>
<commentSection >
<comment>
<user>joe</user>
<date>some date</date>
</comment>
<comment>
<user>john</user>
<date>some date</date>
</comment>
</commentSection>
</news>

My big issue is that I cannot get the tags to close in the right place.
If someone could please include a snippet of code that would show me
how to modify the XML file as above, I would really appreciate it.
Right now, I can't find my mistake. Thank you!

Jul 17 '05 #1
5 1557
Can you post the code that you wrote to try to do this?

Jul 17 '05 #2
Actually I just erased it. But it was basically using these functions:

DOMDocument->createElement( )
DOMNode->appendChild( )

My resulting file ends up resembling this:
<comment><user> john<date>some date</user></date></comment>

So I understand how to do everything except for getting these tags to
close in the right place. any idea what I could be doing wrong? How
could I use those two functions to create a node tree that looks like:
<comment><user> john</user><date>some date</date></comment>

Thanks a lot for your help. I have been racking my brains on this for a
long time

Jul 17 '05 #3
Try this:

<?php
$dom = new DOMDocument("1. 0","iso-8859-1");

/* Create all elements */
$comment = $dom->createElement( "comment"); //root element
$user = $dom->createElement( "user", "Carl");
$date = $dom->createElement( "date", "1-1-2005");

/* Now place them in the correct place in the tree */

$comment->appendChild($u ser); // User is a child of Comment
$comment->appendChild($d ate); // Date is a child of comment
$dom->appendChild($c omment); // Comment is a child of rootdoc

echo $dom->saveXML();
?>

It outputs :
<?xml version="1.0" encoding="iso-8859-1"?>
<comment>
<user>Carl</user>
<date>1-1-2005</date>
</comment>
Make sure you append children according to their place in the hierarchy.
ge********@gmai l.com wrote:
Actually I just erased it. But it was basically using these functions:

DOMDocument->createElement( )
DOMNode->appendChild( )

My resulting file ends up resembling this:
<comment><user> john<date>some date</user></date></comment>

So I understand how to do everything except for getting these tags to
close in the right place. any idea what I could be doing wrong? How
could I use those two functions to create a node tree that looks like:
<comment><user> john</user><date>some date</date></comment>

Thanks a lot for your help. I have been racking my brains on this for a
long time

Jul 17 '05 #4
ok I got it working! Thanks! One question though. When I look at the
resulting XML file, it outputs everything in one long line. How can I
insert newlines so that nodes are on different lines? Thank you!

Jul 18 '05 #5

<ge********@gma il.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
ok I got it working! Thanks! One question though. When I look at the
resulting XML file, it outputs everything in one long line. How can I
insert newlines so that nodes are on different lines? Thank you!


Before the line
$xml_doc->save($fname) ;
insert
$xml_doc->formatOutput = true;
--
Tony Marston

http://www.tonymarston.net

Jul 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11419
by: NOSPAM | last post by:
Hello, I am hoping you can help me with the following problem; I need to process the following steps every couple of hours in order to keep our Sql 2000 database a small as possible (the transaction log is 5x bigger than the db). 1.back-up the entire database 2.truncate the log 3.shrink the log 4.back-up once again. As you may have determined, I am relatively new to managing a sql server database and while I have found multiple...
8
1975
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am not expected to check for everything. At any rate, the problem I am having with the following code is that it does not clear the fields once I press the SEND button. So can anyone here enlighten me as to what is causing the problem.
61
24500
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will 'stretch'</td> <td valign="top" width="300">some data that won't 'stetch'</td> </tr> </table>
13
3294
by: Jalal | last post by:
I am trying to use numeric_limits<double>::min() in an MFC application in Microsoft Visual C++.NET 2003. I am having some difficulties here. The following are the parts of a simple program I wrote and corresponding error massages. I am confused here. Similar codes work fine in Win32 Console Project. Can you please help me to resolve this? Thank you for your helps. Best Regards, Jalal
27
3854
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above seemingly
6
13024
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this is not an ASP.NET related question, but I know this group is knowledgeable and quick with responses. Thanks
4
1316
by: ThunderMusic | last post by:
Hi I want to do just like in the subject ... to have some text and tags between the start and end tags of my user-control. Is there a simple way of doing that? for instance, between the tags, there would be a table with <table>, <tr> and <td> and all the stuff I could need. but everytime I try puting something between the tags, I get an error. My control is all done, only this functionality is missing. Can someone help me please?
3
2237
by: kuiyuli | last post by:
I'm using VC++ .Net to do a simlple program. I tried to use <vector> <list> in the program, and I simply put the folowing lines " #include <list> #include <vector> #include <string> using namespace std; ...... vector <Vector> vectorlist;
7
3631
by: pamelafluente | last post by:
The precious input given by Laurent, Martin, Benjamin about XMLHttpRequest in Javascript, has made me think that perhaps I could improve what I am currently doing by using Ajax. Let's make it simple and schematic, to see if there is a simple Ajax answer to this. A. I have an HTML page which has some pure html/css code representing a GRID of cell. The page may also contain other objects (images, etc). B. On the server I have a windows...
0
9353
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.