The XML object that gets this content based on the XML file that lacks the root, how to solve

Source: Internet
Author: User
Tags string back
An XML object that gets this content based on an XML file that lacks a root
I'm going to deal with a user-submitted XML that is missing the root, which is the original XML standard structure for






.........
.........


But my users submit XML files without root






It is not possible to create an XML object with the Simplexml_load_file function, because the interpreter will error
I just want to load the XML file string into the variable $str, and then add a root string back and forth such as to form a standard XML string content and then use the
simplexml_load_string function to create an XML object
=================
The problem I encountered was the first attempt to load the XML file stream into $STR variable has failed, PHP's file read function seems to have done a bit of hands and feet, the loaded content becomes empty. fread,file_get_contents function is not good, ask who has a brilliant idea?

(Note: Please do not consider manually adding a root to the XML, because this XML is actually a script of a game, a game specifically made of the kind of. I have to do the reading program in a manner compatible with this game data)
The following is the code



Function getxmldom ($filename)
{

$xmlStr =file_get_ Contents ($filename);

if (Strpos ($xmlStr, ' '))
//Preliminary verify that the XML content is the script for the game
{
$xmlStr = ' '. $xmlStr. ' ';
return simplexml_load_string ($XMLSTR);
}
else return false;
}


------Solution--------------------
I tried the function on the judge.

if (Strpos ($xmlStr, ' ')

is modified to

if (Strpos ($xmlStr, '
In addition to simplexml_load_s Tring There is a problem reading XML here, I do not know much about XML need your own analysis of where does not meet the requirements of the old.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.