#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

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


    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 answer is there any solution for this (XML file should be in XML format when opened in Notepad)

    Please reply as soon as possible.

    Thanks in advance.

    Harish Pathak
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,390
    Rep Power
    9645
    Save your PHP script using CRLF line endings.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    9
    Rep Power
    0
    LF works too for me. Since I believe this will trigger a question "How on the Earth can I do that?, I'll answer it in advance - check the documentation of your text editor. For example, if you use Notepad++ to code, it can be done using Settings->Preferences->New Document section.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,390
    Rep Power
    9645
    OP said Notepad a couple times - which happens to dislike anything other than CRLF line endings. Wordpad is smarter.

    It's nice that Microsoft improved/replaced Paint but Notepad needed upgrading more...
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0

    Question Save your PHP script using CRLF line endings.


    Hi,

    ---------------------------------------------------------------------
    Save your PHP script using CRLF line endings.
    ---------------------------------------------------------------------

    How to do that ? Please explain with example.
    Any change I have to made in my code ?

    Thanks in advance.

    Harish Pathak
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    9
    Rep Power
    0
    Originally Posted by requinix
    OP said Notepad a couple times - which happens to dislike anything other than CRLF line endings.
    This is correct. However, it's possible OP is generating code using any other text editor, but for some reason the file needs to be opened by Notepad only.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    9
    Rep Power
    0
    Originally Posted by harishpathak
    Hi,

    ---------------------------------------------------------------------
    Save your PHP script using CRLF line endings.
    ---------------------------------------------------------------------

    How to do that ? Please explain with example.
    Any change I have to made in my code ?

    Thanks in advance.

    Harish Pathak
    See my reply above...
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Hi,

    Still things are not clear to me. Can I open XML file in notepad in XML format ?
    What should I do to do that ?

    Thanks in advance.

    Harish Pathak
  16. #9
  17. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,390
    Rep Power
    9645
    The problem is about line endings. Please educate yourself on what they are.

    When you understand, find out how to change what line endings are used in whatever editor you wrote that PHP code in. You didn't say what it was so I don't know. Change it, re-save the file, run it again, and try opening the XML output.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Hi requinix,

    I am using Dreamweaver CS5, version 11.0. Also our server is Linux where the code (xml file) is lying.
    In Edit >> Preferences >> Code Format >> Line break type is CRLF(Windows).

    Still xml file is not opening in notepad in xml format. when opening in NOTEPAD I am getting the complete tags of the XML in a single line.

    Waiting for your response.

    Thanks in advance.

    Harish Pathak
  20. #11
  21. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,390
    Rep Power
    9645
    If DOM doesn't reuse the line endings from your source then that means it is using your own, and I don't see a way to change them.

    Save the HTML to a string, str_replace the \n line endings with \r\n, and file_put_contents it to the file.

    Or better yet, stop using Notepad.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    6
    Rep Power
    0
    Hi requinix,

    ---------------------------------------------------------------------------------------------------------
    Save the HTML to a string.
    ---------------------------------------------------------------------------------------------------------

    Which HTML you are talking about. Please describe it and or give an example.

    ---------------------------------------------------------------------------------------------------------
    str_replace the \n line endings with \r\n, and file_put_contents it to the file.
    ---------------------------------------------------------------------------------------------------------
    What does the above line mean. I am not getting this point. Please clarify it.
    Also what is "file_put_contents". How to use it ?
    There is no "\n line endings" in my code.

    My 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();
    
    ?>
    And the last point...............

    --------------------------------------------------------------------------------------
    Or better yet, stop using Notepad.
    --------------------------------------------------------------------------------------

    No, we can not stop using Notepad. This (Notepad) is our requirement.
    We have to show XML file contents in Notepad in XML format.
    We need the notepad file also to look like the format of the XML file when opened.

    Please tell me what I do if I have to show/open XML file contents in Notepad in XML format.
    What type of changes I have to made in my code to do that ?
    When I open XML file in Notepad, the text should be in XML format.

    Please answer all of my questions.

    Waiting for your response.
    Please reply as soon as possible.

    Thanks in advance.

    Harish Pathak
  24. #13
  25. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,390
    Rep Power
    9645
    Sorry, I was kinda out of it when I replied. Obviously (or not?) I meant XML, not HTML.

    > I am not getting this point. Please clarify it.
    Do you have any programming skills?

    > Also what is "file_put_contents". How to use it ?
    https://www.google.com/search?q=file_put_contents

    > There is no "\n line endings" in my code.
    Yes there are. There's about 27 of them. Unless you're using a Mac, but that's besides the point.

    > No, we can not stop using Notepad. This (Notepad) is our requirement.
    If the requirement is "be able to use Notepad to view the XML file" then the requirement is stupid. Notepad is one of the worst ways to view XML. Even web browsers are better at it.

    > Please tell me what I do if I have to show/open XML file contents in Notepad in XML format.
    > When I open XML file in Notepad, the text should be in XML format.
    It already is doing that. You don't like it, so I'm trying to tell you how to adjust the output to look prettier.

    > What type of changes I have to made in my code to do that ?
    I've told you everything you need to know: save the XML to a string instead of a file, replace the line endings in the string, and save the modified XML string to the file you want.

IMN logo majestic logo threadwatch logo seochat tools logo