Reading in all of an XML Doc

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kountz
Forum Newbie
Posts: 3
Joined: Thu Dec 17, 2009 10:18 am

Reading in all of an XML Doc

Post by kountz »

I'm trying to read in an XML doc completely, not just the contents of the nodes. I'm able to do it with [ahem] ASP but i can't figure it out in PHP. I've played around with DOMDocument and fgets but the best i've got is just the contents of the nodes - not the complete xml.

The XML file has elements in it that i search for and replace from a database. I then output the final updated XML. The reasoning for this is speed [in terms of on going dev] and simplicity [i'm not great in PHP].

If anyone can help or point me in the right direction that would be great.

For what it's worth here's the ASP that i've used:

Code: Select all

 
 
public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
 
        //write XML here
        StreamReader streamReader = new StreamReader("C:\\Inetpub\\wwwroot\\Admin\\output\\content.xml");
        string strXML = streamReader.ReadToEnd();
        streamReader.Close();
 
        //get translation data for current language
        string strFriendlyName;
        string strTranslation;
        
        SqlConnection SqlConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand SqlComm = new SqlCommand("spGetTranslations", SqlConn);
        SqlComm.CommandType = CommandType.StoredProcedure;
 
        SqlComm.Parameters.Add(new SqlParameter("@LauguageID", globalFunctions.intCount));
 
        SqlConn.Open();
        SqlDataReader SqlDataReader = SqlComm.ExecuteReader();
 
        //replace placeholders with translation data
        if (SqlDataReader.HasRows)
        {
            while (SqlDataReader.Read())
            {
                strFriendlyName = SqlDataReader["FriendlyName"].ToString();
                strTranslation = SqlDataReader["Translation"].ToString();
 
                strXML = strXML.Replace("**" + strFriendlyName + "**", strTranslation);
            }
        }
 
        SqlConn.Close();
        SqlComm.Parameters.Clear();
        SqlDataReader.Close();
 
        context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(0));
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.Write(strXML);
   }
 
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Reading in all of an XML Doc

Post by AbraCadaver »

Well because in ASP you are just reading in the file as text to a string, and in PHP with DOM or SimpleXML you would be loading it as an object that represents the nodes, attributes, values, etc... so that you can work with the XML easily as an object. If all you want is the XML in a string, then use file_get_contents():

Code: Select all

$strXML = file_get_contents("C:\\Inetpub\\wwwroot\\Admin\\output\\content.xml");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kountz
Forum Newbie
Posts: 3
Joined: Thu Dec 17, 2009 10:18 am

Re: Reading in all of an XML Doc

Post by kountz »

Thanks AbraCadaver!
Post Reply