Hey,
I am working on reading and writing the XML DOM File using PHP.
My XML File is In this Format - portfolio category="Category1">
-
<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" />
-
<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" />
-
</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
25 3836
What problem are you having?
My Problem is that how can i read and write the above formatted XML file using PHP, edit the specific existing record and delete.
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.
regards
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.
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?
Yeh i have tried but it shows nothing, here is my code -
$doc = new DOMDocument();
-
$doc->load('../xml/1.xml');
-
$portfolio = $doc->getElementsByTagName( "portfolio" );
-
foreach( $portfolio as $row ){
-
$items = $row->getElementsByTagName( "item" );
-
$item = $items->item(0)->nodeValue;
-
echo $item;
-
}
-
My loaded XML document is in this format -
<portfolio category="Category1">
-
<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" />
-
<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" />
-
</portfolio>
-
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 getElementsByTagName() on <portfolio>? that seems unnecessary to me.
regards
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!
try @Dormilich
i am using getElementsByTagName() 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
@neovantage
DOMElement::getAttribute(); @neovantage
DOMDocument::createElement();
DOMNode::appendChild();
basicly, standard DOM procedures, only they are splitted over several DOM XML classes
i am unable to use that function as i need an example script which use DOMElement::getAttribute(); because i am not that much professional to get the picture of just having the function and performed further operations. Kindly help me out i really need to know about it.
let's make an example.
get the category value (tested and working): - $doc = new DOMDocument();
-
$doc->load($xml); // $xml is your xml file
-
$portfolio = $doc->getElementsByTagName( "portfolio" );
-
$item = $portfolio->item(0)->getAttribute('category');
-
// prints: Category1
-
echo $item;
PS: if you know how to use DOM in Javascript, this is pretty much the same
regards
@Dormilich
Thank you very very very muchhhhhhhhhhh.
It works now i done the same thing with the inner tag and it is working that's cool
My Next question is that " How can i traverse whole recodes so that i can show e.g. when we write the db query then we can fetch array by using while condition with mysql_fetch_array(records) and it automatically terminates once it will be at last record. So is there any method here in XML that i can fetch all the records and it terminate automatically once the last record found.
@neovantage - foreach (DOMNodeList $list as DOMNode $node)
-
{
-
// code comes here
-
}
note that I put in the class names to make clear, what types $list & $node are, of course you call foreach as you usually do.
in some cases you can also use the while loop, depending on the exit definition.
regards
@Dormilich
Sorry i did not get you. i did not get in my XML Document case what is my DOMNodeList $list and what is my DOMNode $node.
I successfully got each attribute and here is my code -
$doc = new DOMDocument();
-
$doc->load('../xml/1.xml'); // $xml is your xml file
-
$portfolio = $doc->getElementsByTagName( "portfolio" );
-
$category_name = $portfolio->item(0)->getAttribute('category');
-
$item = $doc->getElementsByTagName( "item" );
-
$item_title = $item->item(0)->getAttribute('title');
-
$item = $doc->getElementsByTagName( "item" );
-
$item_detail = $item->item(0)->getAttribute('detail');
-
$item = $doc->getElementsByTagName( "item" );
-
$item_company = $item->item(0)->getAttribute('company');
-
and my XML Document is -
<portfolio category="Category1">
-
<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" />
-
<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" />
-
<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 3" type="Item 3 type" detail="Item 3 description" company="Invent Solution 3" year="2008-09" />
-
</portfolio>
-
Now i need to write the foreach loop for this that it traverse from very first record to last record. You told me the method -
1. foreach (DOMNodeList $list as DOMNode $node)
-
2. {
-
3. // code comes here
-
4. }
-
so i want to know that what is my DOMNodeList $list and what is DOMNode $node ?
@Dormilich
I am unable to write a foreach loop in order to traverse all records. Kindly help me out to write a foreach loop in order to traverse all records.
In order to fetch a single record this code i working fine -
$doc = new DOMDocument();
-
$doc->load('../xml/1.xml'); // $xml is your xml file
-
-
$portfolio = $doc->getElementsByTagName( "portfolio" );
-
$category_name = $portfolio->item(0)->getAttribute('category');
-
-
$item = $doc->getElementsByTagName( "item" );
-
$item_title = $item->item(0)->getAttribute('title');
-
-
$item = $doc->getElementsByTagName( "item" );
-
$item_detail = $item->item(0)->getAttribute('detail');
-
-
$item = $doc->getElementsByTagName( "item" );
-
$item_company = $item->item(0)->getAttribute('company');
-
But in order to fetch each record i wrote foreach loop and it is not working
here is my code -
<?
-
$doc = new DOMDocument();
-
$doc->load('../xml/1.xml'); // This is my xml file
-
$portfolio = $doc->getElementsByTagName( "portfolio" );
-
$category_name = $portfolio->item(0)->getAttribute('category');
-
-
$item = $doc->getElementsByTagName( "item" );
-
foreach ($item as $value){
-
$items = $value->getElementsByTagName( "item" );
-
$item_title = $items->item(0)->getAttribute('title');
-
-
$items = $value->getElementsByTagName( "item" );
-
$item_detail = $itemNode->item(0)->getAttribute('detail');
-
-
$items = $value->getElementsByTagName( "item" );
-
$$item_company = $itemNode->item(0)->getAttribute('company');
-
?>
-
<tr class=$class>
-
<td align='center' valign='top' class='text-gray'>1</td>
-
<td align='left' valign='top' class='text-gray'><?=$category_name;?></td>
-
<td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
-
<td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
-
<td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
-
</tr>
-
<? }?>
-
Kindly help me out to resolve my problem.
@neovantage
how to read the manual:
// this is a method definition
DOMNodeList DOMDocument::getElementsByTagName ( string $name )
first value: return type ( DOMNodeList)
second value (before ::): class name ( DOMDocument)
second value (after ::): method name ( getElementsByTagName)
third value: 1st parameter's type ( string)
fourth value: 1st parameter (usually a variable with a descriptive name)
so what does it tell you:
you can use the method getElementsbyTagName on every object that is a DOMDocument (or to be more precise, an instance of the DOMDocument class. note that methods can be inherited!).
the return value of that method is a DOMNodeList (a DOMNodeList instance) which means that you can only apply the methods/properties of the DOMNodeList to the new variable holding the return value.
I don't think I have to discuss parameters....
in the manual every method and every property has such a description. now you can look up all the used methods and see how they correspond.
or specifically in this case:
$doc->getElementsByTagName($name) returns a DOMNodeList instance. (which I named $list) and $node is (as usual in foreach) the variable to work with (in your example it would be a DOMElement instance which is a descendand of the DOMNode class)
to see what variable belongs to what class use var_dump()
@Dormilich
Now i got it and i did that the same thing now but it again not working.
Here is my code -
$doc = new DOMDocument();
-
$doc->load('../xml/1.xml'); // My xml file
-
-
$portfolio = $doc->getElementsByTagName( "portfolio" );
-
$category_name = $portfolio->item(0)->getAttribute('category');
-
-
$item_list = $doc->getElementsByTagName( "item" );
-
-
foreach ($item_list as $node){
-
$item_title = $node->item(0)->getAttribute('title'); $item_detail = $node->item(0)->getAttribute('detail'); $item_company = $node->item(0)->getAttribute('company');
-
?>
-
<tr>
-
<td align='center' valign='top' class='text-gray'>1</td>
-
<td align='left' valign='top' class='text-gray'><?=$category_name;?></td>
-
<td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
-
<td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
-
<td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
-
</tr>
-
<? }?>
-
But this code is not working and keep on giving me the error
Fatal error: Call to undefined method DOMElement::item()
This is my xml File Format -
<portfolio category="Category1">
-
<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" />
-
<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" />
-
</portfolio>
-
Awaitinggggggggggggg!!!! Please help me out
@neovantage
of course. as stated, $node is a DOMElement instance, thus item() does not work on it (item() works on DOMNodeList instances). just omit the item(0) call. the foreach construct takes care of the type conversion (in this case from DOMNodeList to DOMElement) - $item_title = $node->getAttribute('title');
and please use [code] tags when posting code
PS: to see it logically: what type do you get for each single member of a DOMNodeList (a kind of array)? a DOMNode or one of its descendands (all DOM classes except Exception, Implementation, NamedNodemap, Notation, NodeList and XPath)
@Dormilich
Thank you very very very very very much again. It works.
What my next task is about to append a new record at the end of my last record.
As you know my format of XML File so how do i add a new record? Awaiting of your reply.
@neovantage
I advice you to read a DOM tutorial (it doesn't matter if it is a Javascript tutorial about DOM scripting) e.g. XML DOM Introduction (you can skip the part about loading the xml.)
some differences between JS DOM and PHP DOM:
object.method() vs. $object->method()
NodeList[i] vs. $NodeList->item($i)
remember: both languages use the same API
Atli 5,058
Expert 4TB
neovantage,
Please use [code] tags when posting your code examples.
(See How to ask a question)
[code] ... Code goes here... [/code]
Thank you. Moderator @Dormilich
Hey,
I am stuck again while deleting a node using simpleXML.
This is my Formated XML Document - <?xml version="1.0"?>
-
<portfolio category="Category1">
-
<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" />
-
<item thumbimg="http://bytes.com/images/thumb2.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" />
-
<item thumbimg="http://bytes.com/images/thumb3.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 3" type="Item 3 type" detail="Item 3 description" company="Invent Solution 3" year="2008-09" />
-
<item thumbimg="http://bytes.com/images/thumb4.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 4" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
-
<item thumbimg="http://bytes.com/images/thumb5.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 5" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
-
<item thumbimg="http://bytes.com/images/thumb6.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 6" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
-
<item thumbimg="http://bytes.com/images/thumb7.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 7" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
-
<item thumbimg="http://bytes.com/images/thumb8.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 8" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
-
<item thumbimg="http://bytes.com/images/thumb9.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 9" type="Item 1 type" detail="Item 1 description" company="Invent Solution" year="2002-03" />
-
</portfolio>
This is how i traverse my XML Document -
<table cellspacing="1" cellpadding="5" border="0" align="center" width="500">
-
<tr>
-
<td align="center" valign="middle" width="20" class="tab">ID</td>
-
<td align="left" valign="middle" width="100" class="tab">Title</td>
-
<td align="left" valign="middle" width="175" class="tab">Description</td>
-
<td align="left" valign="middle" width="100" class="tab">Company</td>
-
<td align="center" valign="middle" width="100" class="tab">Setting</td>
-
</tr>
-
<?
-
//*********************** LOAD XML Document
-
$xml = new SimpleXMlElement('../xml/1.xml', 0, true);
-
$count=1;
-
//*********************** Multiple Record
-
foreach ($xml->portfolio as $folio){
-
$category_name = $folio->attributes()->category;
-
echo $category_name;
-
}
-
foreach ($xml->item as $item){
-
//unset($item);
-
$item_title = $item->attributes()->title;
-
$item_detail = $item->attributes()->detail;
-
$item_company = $item->attributes()->company;
-
$item_thumbimg = $item->attributes()->thumbimg;
-
//$item_nodeValue = $node->nodeValue;
-
?>
-
<tr>
-
<td align='center' valign='top' class='text-gray'><?=$count;?></td>
-
<td align='left' valign='top' class='text-gray'><?=$item_title;?></td>
-
<td align='left' valign='top' class='text-gray'><div align="justify"><?=$item_detail;?></div></td>
-
<td align='left' valign='top' class='text-gray'><?=$item_company;?></td>
-
<td align='center' valign='top' class='text-gray'><a href="javascript: CautionFolioDelete('<?=$item_thumbimg;?>')"><img src='images/icons/del.jpg' width='14' height='17' border='0' alt='Delete' /></a></td>
-
</tr>
-
<? $count=$count+1;}?>
-
</table>
-
now i want to delete the 4th record. I started a counter named with the variable "$count" and write a javascript function which take "thumbimg" argument and submit the data using Get Technique.
Now when my page is submitted. i wrote code like this given below -
if(isset($_GET['folio_id'])){
-
$folio_id=$_GET['folio_id'];
-
$xml = new SimpleXMlElement('../xml/1.xml', 0, true);
-
foreach($xml->item as $item){
-
if($item['thumbimg'] == $folio_id){
-
//unset($item);
-
unset($item->item); // One method to delete the record
-
-
// Other Method to delete the record
-
$item=dom_import_simplexml($item);
-
$item->parentNode->removeChild($item);
-
}
-
}
-
echo $xml->asXml();
-
}
-
-
It do not delete my record. is there any kinda error in my script while deleting the specific record?
Best regards,
NEOVANTAGE
@neovantage
several:
- SimpleXML can't delete nodes
- unset($item) only deletes the temporary variable $item
- dom_import_simplexml() returns a DOMElement, the original SimpleXML isn't affected by that
the only option I see is using DOMDocument from the beginning.
regards
@Dormilich
Thanks Brother,
It's done. now i can delete the record using -
if(isset($_GET['folio_id'])){
-
$folio_id=$_GET['folio_id'];
-
$xml = new SimpleXMlElement('../xml/1.xml', 0, true);
-
foreach($xml->item as $item){
-
if($item['thumbimg'] == $folio_id){
-
$item=dom_import_simplexml($item);
-
$item->parentNode->removeChild($item);
-
}
-
}
-
$xml->asXml('../xml/1.xml');
-
}
-
The only error was i was writing only
insted of - $xml->asXml('../xml/1.xml');
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 =...
|
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...
|
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...
|
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
...
|
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));...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: teenabhardwaj |
last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: jalbright99669 |
last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
|
by: Matthew3360 |
last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function.
Here is my code.
...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |