Page 1 of 1

Reading in all of an XML Doc

Posted: Thu Dec 17, 2009 10:27 am
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);
   }
 
 

Re: Reading in all of an XML Doc

Posted: Thu Dec 17, 2009 10:40 am
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");

Re: Reading in all of an XML Doc

Posted: Thu Dec 17, 2009 11:30 am
by kountz
Thanks AbraCadaver!