Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing XML data on a new line

Status
Not open for further replies.

chrismassey

Programmer
Aug 24, 2007
264
GB
Hello,

I have a Perl script which divides a large XML files into smaller files. It works great, except if I view the XML file in notepad I can see that the initial tags haven't been placed on a new line.

It should look like this:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<OUTER_TAG>
<FORM>

However, its being displayed as:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?><OUTER_TAG><FORM>

If I use \n to create a new line then its still displayed on the same line but with a tiny square in between which obviously represent the new line character.

How can I produce neat XML when viewed in notepad as a text file?

I have included my script below:

Code:
#! /usr/bin/perl
use strict;
use CGI ':standard';
print "Content-type: text/html\n\n";
my $outer_splitter = 'OUTER_TAG';
my $splitter = 'FORM';
my $xml_file = "xml_file.xml";
open (LOG,"<$xml_file") || die "Cannot Open: $xml_file"; 
my $count = 0; 
my $files_counter = 1; 
my $max_records = 1; 
while (<LOG>){
	if ($count == 0){
		my $filename = 'File_'.$files_counter.'.xml';
		open(LOG2,">$filename") || die "Cannot Open: $filename";
		print "$filename >>> Created (XML)<br>";
		my $filename_pdf = 'File_'.$files_counter.'.pdf';
		open(LOG3,">$filename_pdf") || die "Cannot Open: $filename_pdf";
		print "$filename_pdf >>> Created (PDF)<p>";
		[COLOR=red]print LOG2 '<?xml version="1.0" encoding="ISO-8859-1"?>'."\n";[/color]
		[COLOR=red]print LOG2 "<$outer_splitter>\n";[/color]
		$count++;
	}
	if (grep /<\/$splitter>/, $_ ){
		$count++;
	}
	print LOG2 $_;
	if ($count == $max_records + 1){
		print LOG2 "<\/$outer_splitter>";
		$count = 0;
		$files_counter++;
		close(LOG2);
	}
}
print "<b>Complete<b>";

Thank you very much,

Chris
 
It sounds a bit like a *nix/windows/mac line ending problem.

You mentioned notepad so are you viewing the file on a windows system?

More importantly, are you creating the file on a box other than the one you're viewing it on?
 
Hey, thanks for the response...

As you can maybe see from my script (HTML tags in print statements) I am testing the script on a Web Server. Once I have ran the script I am downloading the XML files it creates to my Windows Desktop and opening them in notepad. I am using the same computer for everything.

Thanks

Chris
 
print LOG2 '<?xml version="1.0" encoding="ISO-8859-1"?>'."\n";

needs to be

print LOG2 '<?xml version="1.0" encoding="ISO-8859-1"?>',"\n";

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Hey, thanks travs... I tried that and I recieve the same result is the result of one of the XML files (note that [] represents the newline character that is displayed in the text file:

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>[]<OUTER_TAG><FORM>
	<SECTION>
		<SECTIONTITLE>Person A</SECTIONTITLE>
		<NAME>Name1</NAME>
		<AGE>22</AGE>
		<SEX>Male</SEX>
	</SECTION>
	<SECTION>
		<SECTIONTITLE>Person B</SECTIONTITLE>
		<NAME>Name2</NAME>
		<AGE>44</AGE>
		<SEX>Male</SEX>
	</SECTION>
	<SECTION>
		<SECTIONTITLE>Person C</SECTIONTITLE>
		<NAME>Name3</NAME>
		<AGE>66</AGE>
		<SEX>Female</SEX>
	</SECTION>
</FORM>
</OUTER_TAG>

Maybe i'm just being fussy and this isn't possible? The client has specifically asked for neat XML viewable in .txt format.

Thanks very much for your suggestions, its much appreciated

Chris
 
Weird.. worked fine for me. (or I'm going crazy). It does seem like the newslines are getting converted somewhere. When you say you are downloading the files you aren't FTPing them are you?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Thats right i'm not using FTP. I'm using 1and1's Webspace Explorer System, and I click the XML file in there and click download. Should I try downloading the file using FTP?

Chris
 
Try using FTP and downloading it as ascii.. it does look as if it has been downloaded as binary.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Ok i've just tried that and I get the ame result. I should mention that if I remove all \n characters the first part of a new XML file looks like this instead (only outer_tag resides on first line):

Code:
<?xml version="1.0" encoding="ISO-8859-1"?><OUTER_TAG>
<FORM>
    <SECTION>
        <SECTIONTITLE>Person A</SECTIONTITLE>
        <NAME>Name1</NAME>
        <AGE>22</AGE>
        <SEX>Male</SEX>
    </SECTION>

etc

Would it be possible to attach the xml file you produced so that I can see how it looks for you? Don't worry if you can't.

Thanks again
 
try:

Code:
print LOG2 '<?xml version="1.0" encoding="ISO-8859-1"?>',"\r\n";



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

Thank you once again, I added \r and it worked! Thank you very much rharsh and travs for your help and suggestions, you are very useful and its much appreciated...

Thanks,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top