Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is This How You Validate XML?

    Just want to make sure if this is how you validate XML. By that I mean checking for errors. Being able to see if it's correctly structured:

    Given the following XML data:
    Code:
    $x = '<fruits>
       <fruit>
          <type>apple</type>
          <color>red</color>
       </fruit>
       <fruit>
          <type>lemon</type>
          <color>green</color>
       </fruit>
    </fruits>';
    This is how I'm validating it:
    PHP Code:
        $success xml_parse($parser$x);
        
        if(
    $success == 0) {
            
    $error_code xml_get_error_code($parser);
            echo 
    "error: ".xml_error_string($error_code)."<BR>";
            echo 
    "row number: ".xml_get_current_line_number($parser)."<BR>";
            echo 
    "column number: ".xml_get_current_column_number($parser)."<BR>";
        }
        else { 
    // validated, now display values    
            
    foreach($fruits as $value){
                echo 
    $value['type']."|".$value['color']."<br>";
            }
        } 
    Just want to make sure if this is how you validate it. Thanks

  • #2
    New Coder
    Join Date
    Dec 2003
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh by the way, do you people know off hand how I could add these characters < > ' " & inside the XML tags? I'm escaping them with their entities (&gt; &lt; etc.) and nothing. When they print out, they print out blank. Any ideas?

  • #3
    me'
    me' is offline
    Senior Coder
    Join Date
    Nov 2002
    Location
    Warwickshire, England
    Posts
    1,229
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by jessjenn
    Oh by the way, do you people know off hand how I could add these characters < > ' " & inside the XML tags? I'm escaping them with their entities (&gt; &lt; etc.) and nothing. When they print out, they print out blank. Any ideas?
    If you've got your own DTD:
    <!ELEMENT elementname (#CDATA) !>
    David House - Perfect is achieved, not when there is nothing left to add, but when there is nothing left to take away. (Antoine de St. Exupery).
    W3Schools | XHTML Validator | CSS Validator | Colours | Typography | HTML&CSS FAQ | Go get Mozilla Now | I blog!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •