XML file download in notepad in correct format like xml file

Questions about XML that are not covered by the other forums should go here.
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi Guys,

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

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
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML file download in notepad in correct format like xml file

Post by adrian »

Hi,

I'm not sure what is the question. Notepad certainly can't help you view an unformatted XML document.
You don't seem to be using Oxygen, you are downloading the XML file from somewhere and since it is unformatted it is impractical to read with Notepad.
The XML file needs to be formatted (or viewed) with an XML editor or processed with an XML formatting tool, if you want to view it with Notepad.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi adrian,
------------------------------------------------------------------------------------------------------------------------------------------------------
The XML file needs to be formatted (or viewed) with an XML editor or processed with an XML formatting tool, if you want to view it with Notepad.
------------------------------------------------------------------------------------------------------------------------------------------------------

How to do that using XML editor and or XML formatting tool ? Could you please give an example ? what are all xml formatting tool ?

Please reply as soon as possible.

Thanks in advance.

Harish Pathak
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML file download in notepad in correct format like xml file

Post by adrian »

Hi,
harishpathak wrote:------------------------------------------------------------------------------------------------------------------------------------------------------
The XML file needs to be formatted (or viewed) with an XML editor or processed with an XML formatting tool, if you want to view it with Notepad.
------------------------------------------------------------------------------------------------------------------------------------------------------
How to do that using XML editor and or XML formatting tool ? Could you please give an example ?
Open the XML file in Oxygen XML Editor (or Author, or Developer) and use the Document > Source > Format and Indent (Ctrl+Shift+P) action. This will break the lines before elements and insert indents before elements, so that they are aligned and thus readable.
harishpathak wrote:what are all xml formatting tool ?
Search the web for "xml formatting tool" and you'll find your answer.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi adrian,

----------------------------------------------------------------------------------------------------------------------------------------------------
Open the XML file in Oxygen XML Editor (or Author, or Developer) and use the Document > Source > Format and Indent (Ctrl+Shift+P) action. This will break the lines before elements and insert indents before elements, so that they are aligned and thus readable.
----------------------------------------------------------------------------------------------------------------------------------------------------
I have done that using XML Editor but this does not show notepad file to look like the format of the XML file when opened.

Waiting for your reply.

Harish Pathak
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML file download in notepad in correct format like xml file

Post by adrian »

harishpathak wrote:I have done that using XML Editor but this does not show notepad file to look like the format of the XML file when opened.
What do you have there? Not sure what you're looking at. Also not sure what are your expectations.

e.g. Format and indent takes something like

Code: Select all

<root><tag>1</tag><tag>2</tag></root>
and turns it into

Code: Select all

<root>
<tag>1</tag>
<tag>2</tag>
</root>
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi adrian,
----------------------------------------------------------------------------------------------------------------------------
What do you have there? Not sure what you're looking at. Also not sure what are your expectations.
----------------------------------------------------------------------------------------------------------------------------
Thing is very clear, I am generating a XML file (welcome.xml) through PHP. The PHP code is as -

Code: Select all


<?php 
$xml = file_get_contents('test/Trans.xml', true);
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->formatOutput = true;
$xmlDoc->loadXML ( $xml );
$xmlDoc->save('test/welcome.xml');
echo $xmlDoc->saveXml();
?>
Well, when I open XML file (welcome.xml) in browser it shows the right format i.e. XML format. Also when I open XML file (welcome.xml) in Oxygen XML Editor it shows the right format i.e. XML format, I have done "Format and Indent" in Oxygen XML Editor" , but when I open the same file in Notepad it does not shows the right format i.e XML format. I want the XML file also to look like the format of the XML file when opened in Notepad.
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)
---------------------------------------------------------------------------------------------------------------------------------------------------
XML Format >>
-------------------------------
<?xml version="1.0" ?>
- <SalesOrders>
- <Order ID="AMT1579698">
- <Header>
<CustomerNumber>73-862</CustomerNumber>
<CustomerAlias>862</CustomerAlias>
<CustomerPO>AMT1579698</CustomerPO>
<FOB>248753480</FOB>
<Comment>00800539/2560/1231/4603</Comment>
<UserName>patterk@amtrak.com</UserName>
<ConfirmTo>Hearns, Anrechio</ConfirmTo>
<TaxCode>NONTAX</TaxCode>
<SalesTax>0.00</SalesTax>
<Freight>0.00</Freight>
<ShipVia>FDXGRD/3RDPRTY</ShipVia>
<SalesPersonID>73-318P</SalesPersonID>
<TermsCode>00</TermsCode>
<ShipDate>9/6/2017</ShipDate>
<ShipZone>No</ShipZone>
<CreditCardSale>No</CreditCardSale>.............................................
---------------------------------------------------------------------------------------------------------------------------------------------------


Waiting for your reply.

Thanks in advance.

Harish Pathak
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML file download in notepad in correct format like xml file

Post by adrian »

Hi,
Also when I open XML file (welcome.xml) in Oxygen XML Editor it shows the right format i.e. XML format, I have done "Format and Indent" in Oxygen XML Editor"
Did you save the file after formatting it in Oxygen?
The file you have formatted and saved in Oxygen should look the same when you open it with Notepad. An XML file is still a text file.

What you call "XML format" looks like an XML document formatted with one element per line.
Is it possible in any way. (XML file contents should be in XML format when opened in Notepad)
What does it look like now when you open it with Notepad?

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi Adrian,

1.)
------------------------------------------------------------------------
Did you save the file after formatting it in Oxygen?
------------------------------------------------------------------------
Yes I had saved it, but the file does not show in the right format i.e. XML format when opened in Notepad.

2.)
------------------------------------------------------------------------
The file you have formatted and saved in Oxygen should look the same when you open it with Notepad. An XML file is still a text file.
------------------------------------------------------------------------
No, the XML file does not show in the right format i.e. XML format when opened in Notepad.

3.)
------------------------------------------------------------------------
What does it look like now when you open it with Notepad?
------------------------------------------------------------------------

When I open the XML file in Notepad (I am getting the complete tags of the XML in a single line.) the format is as -

<?xml version="1.0"?><SalesOrders> <Order ID="AMT1579698"> <Header> <CustomerNumber>73-862 </CustomerNumber> <CustomerAlias>862</CustomerAlias> <CustomerPO>AMT1579698</CustomerPO> <FOB>248753480</FOB> <Comment>00800539/2560/1231/4603</Comment> UserName>patterk@amtrak.com</UserName> <ConfirmTo>Hearns, Anrechio</ConfirmTo> <TaxCode>NONTAX</TaxCode> <SalesTax>0.00</SalesTax> <Freight>0.00</Freight> <ShipVia>FDXGRD/3RDPRTY</ShipVia> <SalesPersonID>73-318P</SalesPersonID> <TermsCode>00</TermsCode>


Waiting for your reply.

Thanks in advance.

Harish Pathak
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML file download in notepad in correct format like xml file

Post by adrian »

Hi,

Your XML file is probably using Linux/Unix line separators ('\n'), but Notepad is a "simple" Windows application and doesn't know what they are, it expects Windows line separators ('\r\n'). If you open it with WordPad, it should look fine.
To force Windows line separators in Oxygen, go to Options > Preferences, Global, set the Line separator to Windows-like and clear the box for Detect the line separator on file open. Open the file, make a change and save it.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi Adrian,

First of all, thanks for your prompt reply. you are great. Finally my problem is solved and now xml file opened in Notepad showing in the right format i.e. XML format. Again Thanks, Thanks, Thanks. My problem is sorted out.

I have made the changes in Oxygen XML Editor that you told, by the way after making changes I try to save it but it doesn't save,save option was disabled after making changes in Oxygen XML Editor, it had the option to Save As and I did it (Save as). I save as my file changing another name (in Oxygen XML Editor) and had opened in Notepad and it workes.

One thing I have to tell you that I am using Trial version of Oxygen XML Editor.
---------------------------------------------------------------------------------------------------------------------------------------
Your XML file is probably using Linux/Unix line separators ('\n'), but Notepad is a "simple" Windows application and doesn't know what they are, it expects Windows line separators ('\r\n'). If you open it with WordPad, it should look fine.
---------------------------------------------------------------------------------------------------------------------------------------

You are absolutely right,Thanks a lot for your solution oriented reply.
Our Server is Linux. And Notepad is a "simple" Windows application.

Thanks

Harish Pathak
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi Adrian,

Now the things have been changed. Requirement has been changed now.
I need the same result without using any XML formatting tool i.e. XML Editor or other.

I am generating a XML file (welcome.xml) through PHP. The PHP code is as -

Code: Select all


<?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();
?>
I need the notepad file also to look like the format of the XML file when opened without using any formatting tool.

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

We don't want to use any formatting tool.

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
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: XML file download in notepad in correct format like xml file

Post by adrian »

Hi,

You'll have to change the line terminators directly in the PHP code. PHP is not my specialty, so you'll have to manage the details yourself.
Anyway, what you need to do is:
1. Save the XML to a string variable.
2. Replace in the string "\n" (Unix terminator) with "\r\n" (Windows terminator)
3. Save the string to a file (preserving the line terminators).

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

-----------------------------------------------------------------------------------------
Replace in the string "\n" (Unix terminator) with "\r\n" (Windows terminator)
-----------------------------------------------------------------------------------------

Hi Adrian,

Where should I apply this in my code ?
I am not getting this point.
Please explain.


Thanks in advance

Waiting for your reply.

Harish Pathak
harishpathak
Posts: 8
Joined: Fri Sep 22, 2017 10:58 am

Re: XML file download in notepad in correct format like xml file

Post by harishpathak »

Hi Adrian,

Please reply Adrian.

Thanks

Harish Pathak
Radu
Posts: 9046
Joined: Fri Jul 09, 2004 5:18 pm

Re: XML file download in notepad in correct format like xml file

Post by Radu »

Hi,

There's nothing wrong with XML documents saved via PHP to have "\n" line separators instead of the Windows-specific "\r\n". It's just that Notepad is a primitive application which only looks for "\r\n" when deciding line breaks. If you want a better free application you can try to install and use Notepad++.
Anyway, you can also save your XML DOM element to a string using "saveXml()" and afterwards use PHP replace functionality to replace "\n" with "\r\n", something like:

Code: Select all


$output = str_replace("\n", "\r\n", $input);
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply