Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question XML file open in notepad in correct format like xml file

    Hi Guys,

    We have a requirement to download the internal table data as XML file.

    I am generating a XML file (welcome.xml) through PHP. The PHP code is as -
    Code:
    <?php  
    $xmlString = '<?xml version="1.0" encoding="UTF-8"?>
        <urlset 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" 
    	xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" 
    	xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
            <url>
                <loc>http://www.codexworld.com</loc>
                <lastmod>2016-07-04T07:46:18+00:00</lastmod>
                <changefreq>always</changefreq>
                <priority>1.00</priority>
            </url>
        </urlset>';
    
    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->loadXML($xmlString);
    $dom->formatOutput = TRUE;
    //Save XML as a file
    $dom->save('test/welcome.xml');
    
    //View XML document
    
    echo $dom->saveXml();
    
    ?>
    Able to download the XML file in correct format, but when opened in NOTEPAD we are getting the complete tags of the XML in a single line.

    We need the notepad file also to look like the format of the XML file when opened.

    Please reply as soon as possible.

    Thanks in advance.

    Harish Pathak

  2. #2
    Regular Coder
    Join Date
    Sep 2017
    Location
    Lithuania
    Posts
    172
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Ran your code on my server, works perfectly here (file opened in text editor has new lines as it should).

  3. #3
    New to the CF scene
    Join Date
    Sep 2017
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question XML file tobe open in notepad in correct format like xml format

    Hi,

    ------------------------------------------------------------------------------------------------------------------------
    Ran your code on my server, works perfectly here (file opened in text editor has new lines as it should).
    ------------------------------------------------------------------------------------------------------------------------

    Which text editor you used ? Was it Notepad ?

    Right now when I open XML file (Generate through PHP ) in NOTEPAD I am getting the complete tags of the XML in a single line.

    As I have mentioned in my question that I need the notepad file also to look like the format of the XML file when opened.

    Please answer is there any solution for this (XML file should be in XML format when opened in Notepad)

    Is it possible in any way. (XML file contents should be in XML format when opened in Notepad)

    Waiting for your reply.

    Thanks in advance.

    Harish Pathak

  4. #4
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,427
    Thanks
    4
    Thanked 488 Times in 476 Posts
    Relying on formatting retention when you're running it past a parser is NOT a realistic expectation. That information -- the meaningless whitespace -- SHOULD be deleted or not translated into the DOMDocument since those are NOT DOM elements and/or attributes.

    Likewise the formattedOutput option does NOT force attribute formatting, only tag level. As such your output SHOULD look like this:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    	<url>
    		<loc>http://www.codexworld.com</loc>
    		<lastmod>2016-07-04T07:46:18+00:00</lastmod>
    		<changefreq>always</changefreq>
    		<priority>1.00</priority>
    	</url>
    </urlset>
    The parameters condensed to a single line but the tags indented properly. NOTE the XML prologue does not cause an indent as it's a SIBLING to the urlset, NOT a wrapper of it!!!

    ... and no, there's no easy way around that.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •