473,884 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read and write the XML DOM with PHP

245 New Member
I am working on reading and writing the XML DOM File using PHP.
My XML File is In this Format

Expand|Select|Wrap|Line Numbers
  1. portfolio category="Category1">
  2.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  3.     <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  4. </portfolio>
Now i am unable to read and write data in this XML Format. Can any one help me out that how to write, read, edit and delete the data from this XML file using PHP. I will be very grateful who will help me in this regard. I am badly stuck. Kindly help me please.

Best regards,
Mohsin Rafique
Dec 30 '08 #1
25 4056
code green
1,726 Recognized Expert Top Contributor
What problem are you having?
Dec 30 '08 #2
8,658 Recognized Expert Moderator Expert
option 1 – PHP: DOMDocument - Manual
(esp. the loadXML() and save() methods, XML manipulation is done with the standard DOM functions)

option 2 – PHP: SimpleXML - Manual
(good for reading simple xml documents)

option 3 – PHP: XSL - Manual
(applying all changes in one go, XSLT knowledge (some) required)

Dec 30 '08 #3
245 New Member
My Problem is that how can i read and write the above formatted XML file using PHP, edit the specific existing record and delete.
Dec 30 '08 #4
8,658 Recognized Expert Moderator Expert
that depends on you specific task. there might be situations when it's better to do XSLT (xml structure changes) and some where a simple DOM method will do.

as for loading, changing and saving your xml, read the manuals. they explain it pretty straightforward .

Dec 30 '08 #5
245 New Member
The thing is my XML is not in the format as the examples given using SimpleXML or the examples given in PHP: DOMDocument. Can you write a sample code for me that how can i read this formatted document and add a new record or edit existing record using PHP.
Dec 30 '08 #6
8,658 Recognized Expert Moderator Expert
have you actually tried to load your xml? (the samples are samples after all.... my own xml files are different too, they even have namespace, but they work)

if it doesn't work, what error message do you get?
Dec 30 '08 #7
245 New Member
Yeh i have tried but it shows nothing, here is my code
Expand|Select|Wrap|Line Numbers
  1. $doc = new DOMDocument();
  2. $doc->load('../xml/1.xml');
  3. $portfolio = $doc->getElementsByTagName( "portfolio" );
  4. foreach( $portfolio as $row ){
  5. $items = $row->getElementsByTagName( "item" );
  6.                                                                                      $item = $items->item(0)->nodeValue;
  7.                                                                                     echo $item;
  8.                                                                                }
My loaded XML document is in this format
Expand|Select|Wrap|Line Numbers
  1.  <portfolio category="Category1">
  2.      <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 1" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03"  />
  3.      <item thumbimg="http://bytes.com/images/thumb1.jpg" largeimg1="http://bytes.com/images/large1_1.jpg" largeimg2="http://bytes.com/images/large1_2.jpg" largeimg3="http://bytes.com/images/large1_3.jpg" title="Item 2" type="Item 2 type" detail="Item 2 description" company="Invent Solution 2" year="2007-08" />
  4.  </portfolio>
Dec 30 '08 #8
8,658 Recognized Expert Moderator Expert
ok, that's something to work with.

do you know if you actually enter the foreach loop? (i.e. if you have a non-empty result set)

why using getElementsByTa gName() on <portfolio>? that seems unnecessary to me.


PS: what do you expect item(0)->nodeValue to be? you're requesting the value of the element node (which in DOM is strictly speaking non-existant) you could get the value of the text node (which would be $node->firstChild->nodeValue). I think you get the text node's value.......... ...... of an empty element!

Expand|Select|Wrap|Line Numbers
  1. var_dump($item);
Dec 30 '08 #9
245 New Member
i am using getElementsByTa gName() on <portfolio> because i need to read the category name and as you can see the tag <item> has element title and title value but i am unable to read it. How can i read the title, type, detail, company, year, thumbimg, largeimg1, largeimg2, largeimg3 values? and how can i add a new record which append at the end of my alredy exisiting record
Dec 30 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE) p.stdin.write("hostname\n") however, it doesn't seem to work. I think the cmd.exe is catching it.
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate the object and set default blank/null values), and then does all the Db access and number crunching to populate the new object. The factory returns the fully populated object to the caller. All the fields in the object are private, but have...
by: Juan Manuel Ramollino | last post by:
Greetings everyone! I am creating a webcontrol that reads one or more directorie's content and displays all the filenames (tipically PDFs or PPTs) so that the user can select the desired one and see the content. We call it FileBrowser (oh so clever) The purpose of this control is to display security bulletins, tutorials, user manuals on a corporate Intranet. So far so good.
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write and then read: string1, integer1, double1
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
by: Arpan | last post by:
The contents of a text file are as follows: The Quick Brown Fox Jumped Over The Lazy Dog. Note that there isn't any space at the end of each of the 3 lines. Now when I do this:
by: dosworldguy | last post by:
I have been having a very peculiar issue from a long time. I have an application where multiple clients read from a shared set of files. When a record is changed, sometimes the win9x clients still read the old data (if it was read earlier) and this is causing data corruption. WinNT clients inlcuding windows2000 & XP do not have this issue. The program is complied in VC++, console mode. I am unable to understand the cause. I flush the...
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and write using fstream is not not working.If i close the file and open it again it works. #include <iostream> #include <sys/stat.h> #include <fstream> int main(){ fstream fs; char write= {"A0000.label"};
by: asit dhal | last post by:
hello friends, can anyone explain me how to use read() write() function in C. and also how to read a file from disk and show it on the monitor using onlu read(), write() function ??????
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data = file.read() print "bytes read ", len(data) ---
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
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,...
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...
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...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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

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.