How can I obtain the XML object of this content based on the XML file without the root?

Source: Internet
Author: User
The XML object that obtains the content based on the XML file that lacks the root. I want to process the XML file submitted by the user, but the root is missing, that is, the original XML standard structure is & lt; root & gt; & nbsp; & lt; node1 & gt; & nbsp; & lt;/node1 & gt; & nbsp; & lt; node2 & gt; & nbsp; get the XML object of the content based on the XML file without the root.
I want to process a kind of XML submitted by the user, which lacks the root, that is, the original XML standard structure is






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


However, my users submit XML files without a root






Therefore, it is impossible to use the simplexml_load_file function to create an XML object, because the interpreter reports an error.
I want to load the XML file string to the variable $ str, and then add a root string before and after Form a standard XML string and use it again.
Simplexml_load_string function to create an XML object
========================
The problem I encountered was that the first attempt was to load the xml file stream to the $ str variable, and the PHP file reading function seemed to have done something, empty all loaded content. fread and file_get_contents functions do not work. Which of the following is a highly recommended method?

(Note: Please do not consider manually adding a root on XML, because this XML is actually a script of a game, and a game is specially crafted into samples .. I have to read the program with compatibility with the game data)
The following code is used:



Function GetXMLDOM ($ filename)
{

$ XmlStr = file_get_contents ($ filename );

If (strpos ($ xmlStr ,' '))
// Verify whether the XML content is the script of the game.
{
$ XmlStr =' '. $ XmlStr .' ';
Return simplexml_load_string ($ xmlStr );
}
Else return false;
}


------ Solution --------------------
I tried the function to identify problems.

If (strpos ($ xmlStr ,' '))

Modify

If (strpos ($ xmlStr ,'
In addition, reading xml in simplexml_load_string may cause problems. I don't know much about xml, so you need to analyze what does not meet the requirements by yourself.

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.