473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to fix errors in XML file using php program?

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <note>
  3.     <to>Tove</to>
  4.     <from name=ahmad>Jani</from>
  5.     <heading>Reminder
  6.     <body>Don't forget me this weekend!</body>
  7. </note>

Above is my xml file which contains two errors.
1. the attribute value of from tag in not enclosed in "".
2. heading tag has no ending tag.

i want a php program to fix these errors automatically.
enclose the attribute value in "" and place an ending tag for heading tag.

Any one who can help me???
Dec 23 '10 #1
4 2610
code green
1,726 Expert 1GB
That is like asking somebody to write a C++ program to correct all my PHP syntax errors.
It is a nice concept, and in theory not impossible with string manipulation and look-up tables.
But how could every conceivable scenario be handled?
Have you actually given this any thought?
Dec 24 '10 #2
I have found the code for the above issue.
But now the problem is of attributes.
suppose i have an xml file like the above and the tag have the attribute name = ahmad. the value of the attribute is not enclosed in "double quotes".
Now i want a php program to enclose the value of the attribute "name" in "double quotes".
Any one who can do this???
Dec 24 '10 #3
AutumnsDecay
170 100+
If you have php creating the xml file you could do something like:

Expand|Select|Wrap|Line Numbers
  1.  
  2. ... PREVIOUS LINES OF CODE ...
  3.  
  4. print '<from name=\"' . $name . '\">TEXT</from>';
  5.  
  6.  
Putting a backslash before quotes is known as 'escaping'. PHP will gnore these as potential statement breaks, and physically allow the quotes to be generated, instead of breaking the code.
Dec 24 '10 #4
but i want to dynamically enclosed the value of attribute(if exist) in "double quotes".

Because my program will no know the tags of the xml file.\
I have written the following code but its not working properly. if you can correct this then please tell me.

Herer is my code


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $xml = new DOMDocument();
  3. $xml->recover = true;
  4. $xml->load('test.xml');
  5. $node = $xml->getElementsByTagName('*');
  6.  
  7. $nodename = "";
  8. $node_name = array();
  9. $att = "";
  10. $attr_name = "";
  11. $attr_value = "";
  12.  
  13. foreach($node as $value)
  14. {
  15.  
  16.  
  17. echo "Node Name is :".$value->nodeName;
  18.  
  19.  
  20. $nodename = $value->nodeName;
  21. $att = $value->getAttributeNode('name');
  22.  
  23.  
  24. echo "<HTML><Head>";
  25. echo "<title> Getting Attribute Example</title>";
  26. echo "</Head><body><B>";
  27. echo "Node Name is :".$att->name;
  28. echo "<BR>Attribute Value is :".$att->value;
  29. echo "<br /><br /></B></body></HTML>";
  30.  
  31. $attr_name = $att->name;
  32. $attr_value = '"' . $att->value . '"';
  33. if($value->hasAttribute('name'))
  34. {
  35. $value->removeAttributeNode($att);
  36.  
  37. $value->setAttribute(new DOMAttr($att->name, $att->value));
  38. }
  39. echo $xml->saveXML();
  40. }
  41.  
  42. $xml->save('temp2.xml');
  43. ?>
Dec 24 '10 #5

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

Similar topics

6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
2
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my...
3
by: curt.bathras | last post by:
I am trying to open and read a file using the following: BufferedStream stream = new BufferedStream(File.OpenRead(aFilename)); If the file specified by aFilename is being used by another...
2
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question in relation to opening files from C#. I have a Windows form where I allow the user to type in a file extension in a text box (e.g., "xls"). I then take...
6
by: gauravkhanna | last post by:
import java.io.*; class Automation1 implements Runnable { int iterationCount=0; Thread t; FileReader fr; //FileWriter fw; BufferedReader br; ...
3
by: SM | last post by:
Hello, Im trying to access elements in my XML file using the JavaScript DOM but i'm not sure how. I use AJAX to access the XML and then use the responseXML property to access the XML file data. I...
2
by: charlesbritto | last post by:
A C++ program for counting individual string in a text file using file handling functions., for ex: if the text file contains, am a boy,am studying +2,am from chennai Now the result shoud...
3
by: sophia | last post by:
Dear all, the following is the file compression program ,using elimination of spaces, which I saw in a book #include<stdio.h> #include<stdlib.h> int main(int argc,char * argv) {
1
by: rparimi | last post by:
Hello pythoners, When I create temporary file using the tempfile module, and forkI) later on in my program, I always see errors when the program exits. Is this because the child process deletes...
6
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.