Pages

Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Sunday, October 16, 2011

XML Formatter

Many times I've searched for tools/plugins/libraries to format/indent/... small XML files. A tool to do that can be easily done with classes from System.Xml namespace
try{      
 // Format XML file
 XmlDocument^ doc = gcnew XmlDocument;
 
 char tmpFileName[MAX_PATH] = "";
 GetTempFileName(".", "bak_1", 0, tmpFileName);
 
 // convert unmanaged -> managed
 String^ srcFile = gcnew String(szFileName);
 String^ tmpFile = gcnew String(tmpFileName); 
 
 File::Copy(srcFile, tmpFile, true);
 try{
  doc->Load(tmpFile);
 }
 catch(XmlException^ e1) {
  //convert Managed -> unmanaged
  TCHAR* errMsg = (TCHAR*)(void*)Marshal::StringToHGlobalAnsi(e1->Message);
  MessageBox(NULL, errMsg, "Error", 0);
  return 0;
 }

 XmlWriterSettings^ xws = gcnew XmlWriterSettings;     
 xws->Indent = true;
 xws->CheckCharacters = false;

 XmlWriter^ writer = XmlWriter::Create(srcFile, xws);      
 doc->Save(writer);
 writer->Close();

 File::Delete(tmpFile);
}
catch(Exception ^e) {
 MessageBox(NULL, szFileName, "Exception while converting following files:", 0);
}
MessageBox(NULL, "Converted", "ok", 0);


How nice would be to build your own small tools and adapt them when you need to.   A (very) small step towards being a self sustainable programmer is this :)
http://code.google.com/p/cool-xml/

Thursday, January 7, 2010

Stylesheets in Firefox and Chrome

  1. Background

              A basic security idea for browser's security is the same-origin policy (discussion here) which protects web sites from one another. For example, the same-origin policy stops a news site from reading the contents of your Gmail inbox or use Javascript to access information from other frames in the same window. 
    Different browsers have different security approaches:

    Google Chrome :
    • prevents users from clicking Internet-based hyperlinks to local web pages 
    • blocks local web pages from reading the contents of arbitrary web sites
    Firefox:
    • blocks local web pages from reading Internet pages
    • restricts a local web page to reading only files in the same directory, or a subdirectory
  2. Problem: 
              If you have an .xml file that references an .xls,  (both files are locale),  when I open it in Firefox the stylesheet is not applied. The contents appear unformatted.  If I open it with Chrome a blank page appears,  and I get an error on the Developer Console:
    Unsafe attempt to load URL file:///C:/test.xsl from frame with URL file:///test.xml. Domains, protocols and ports must match.

  3. Workaround:

    • Firefox: This site explains how to enable local documents to have access to all other local documents, including directory listings. And security implications, recommended settings, etc.
    • From a discussion on issue 39616, a workaround for Chrome is to start it with following 2 flags:  --enable-file-cookies --disable-web-security. 

  4. References: