How to fix errors in XML file using php program?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ahmad Nawaz
    New Member
    • Dec 2010
    • 4

    How to fix errors in XML file using php program?

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <note>
        <to>Tove</to>
        <from name=ahmad>Jani</from>
        <heading>Reminder
        <body>Don't forget me this weekend!</body>
    </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???
    Last edited by Dormilich; Dec 25 '10, 10:19 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    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?

    Comment

    • Ahmad Nawaz
      New Member
      • Dec 2010
      • 4

      #3
      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???

      Comment

      • AutumnsDecay
        New Member
        • Mar 2008
        • 170

        #4
        If you have php creating the xml file you could do something like:

        Code:
        ... PREVIOUS LINES OF CODE ...
        
        print '<from name=\"' . $name . '\">TEXT</from>';
        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.

        Comment

        • Ahmad Nawaz
          New Member
          • Dec 2010
          • 4

          #5
          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


          Code:
          <?php
          $xml = new DOMDocument();
          $xml->recover = true;
          $xml->load('test.xml');
          $node = $xml->getElementsByTagName('*');
          
          $nodename = "";
          $node_name = array();
          $att = "";
          $attr_name = "";
          $attr_value = "";
          
          foreach($node as $value)
          {
          
          
          echo "Node Name is :".$value->nodeName;
          
          
          $nodename = $value->nodeName;
          $att = $value->getAttributeNode('name');
          
          
          echo "<HTML><Head>";
          echo "<title> Getting Attribute Example</title>";
          echo "</Head><body><B>";
          echo "Node Name is :".$att->name;
          echo "<BR>Attribute Value is :".$att->value;
          echo "<br /><br /></B></body></HTML>";
          
          $attr_name = $att->name;
          $attr_value = '"' . $att->value . '"';
          if($value->hasAttribute('name'))
          {
          $value->removeAttributeNode($att);
          
          $value->setAttribute(new DOMAttr($att->name, $att->value));
          }
          echo $xml->saveXML();
          }
          
          $xml->save('temp2.xml');
          ?>
          Last edited by Dormilich; Dec 25 '10, 10:21 AM. Reason: please use [CODE] [/CODE] tags when posting code

          Comment

          Working...