Valid XML for SimpleXML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bleen

    #1

    Valid XML for SimpleXML

    I'm trying to use SimpleXML but I've run into a conundrum. Every day
    an XML file is generated that this script grabs and manipulates. How
    can I check that the XML has no problems before creating my
    SimpleXMLelemen t object. Here's what I mean:

    // this is the code in question:
    $file_topstory = /some/xml/file.xml
    $top_story_xml = new SimpleXMLElemen t($file_topstor y, NULL, TRUE);

    If the XML doc contains (for example) a URL with an '&' in it then the
    script fails. I'd like to do something like this:

    if(is_valid_xml ($file_topstory )){
    $top_story_xml = new SimpleXMLElemen t($file_topstor y, NULL, TRUE);
    }else{
    // ERROR
    }

    How can i accomplish this?

  • Janwillem Borleffs

    #2
    Re: Valid XML for SimpleXML

    bleen wrote:
    I'm trying to use SimpleXML but I've run into a conundrum. Every day
    an XML file is generated that this script grabs and manipulates. How
    can I check that the XML has no problems before creating my
    SimpleXMLelemen t object.
    >
    SimpleXMLElemen t throws an exception when the XML is invalid:

    try {
    $xml = new SimpleXMLElemen t(.....);
    } catch (Exception $e) {
    // Unparseable XML
    }


    JW


    Comment

    • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

      #3
      Re: Valid XML for SimpleXML

      bleen wrote:
      How can I check that the XML has no problems before creating my
      SimpleXMLelemen t object.
      Try tidy. Either as a standalone program, or as a PHP module.

      --
      ----------------------------------
      Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

      "You know "that look" women get when they want sex? Me neither."
      --Steve Martin

      Comment

      • ZeldorBlat

        #4
        Re: Valid XML for SimpleXML

        On Jul 18, 3:53 pm, bleen <blee...@gmail. comwrote:
        I'm trying to use SimpleXML but I've run into a conundrum. Every day
        an XML file is generated that this script grabs and manipulates. How
        can I check that the XML has no problems before creating my
        SimpleXMLelemen t object. Here's what I mean:
        >
        // this is the code in question:
        $file_topstory = /some/xml/file.xml
        $top_story_xml = new SimpleXMLElemen t($file_topstor y, NULL, TRUE);
        >
        If the XML doc contains (for example) a URL with an '&' in it then the
        script fails. I'd like to do something like this:
        >
        if(is_valid_xml ($file_topstory )){
        $top_story_xml = new SimpleXMLElemen t($file_topstor y, NULL, TRUE);
        >
        }else{
        // ERROR
        }
        >
        How can i accomplish this?
        According to the manual at <http://www.php.net/manual/en/
        function.simple xml-element-construct.php>

        "Produces an E_WARNING error message for each error found in the XML
        data and throws an exception if errors were detected."

        So, suppress the error and catch the exception:

        try {
        $top_story_xml = @new SimpleXMLElemen t($file_topstor y, NULL,
        TRUE);
        } catch (Exception $e) {
        //some error occured
        }

        Comment

        Working...