开发者

Inexplicable linebreaks in xml file that cannot be removed

开发者 https://www.devze.com 2023-01-31 07:32 出处:网络
So I have a sql database that generates an xml file. This xml file gets put on a webserver and then par开发者_开发技巧sed by a php file. Unfortunately randomly sometimes there will be a line break in

So I have a sql database that generates an xml file. This xml file gets put on a webserver and then par开发者_开发技巧sed by a php file. Unfortunately randomly sometimes there will be a line break in the xml file that causes it to be unable to be parsed.

http://imgur.com/jNgiE

As you can see from line 12-14. There is a linebreak. But I have no idea why. And I even wrote a script to remove carriage returns and newline characters but the linebreaks still remain. Anyone have any ideas?

$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$inputXML);
$fixedXML = str_replace("  ","",$inputXML);
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);


Your code to remove linebreaks is broken. make the following changes:

$inputXML = file_get_contents("ukso.xml");
$fixedXML = str_replace("\r","",$inputXML);
$fixedXML = str_replace("\n","",$fixedXML); // note: reference the correct variable here
$fixedXML = str_replace("  ","",$fixedXML); // and here.
$myFile = "ukso.xml";
print $fixedXML;
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $fixedXML);
fclose($fh);

The code as you have it is only trimming out double spaces.


I see you're using notepad++

Go into view-show symbol-show all characters. That will tell you exactly what characters there are in the file.

It's possible that the line breaks are also due to the line being too long otherwise. XML files are usually designed to have line breaks at the end of tags, and you may be running into a situation where the line length is too long somewhere in one of the programs. (Often they create a fixed buffer and read in as much as possible, and if it's too long the program will chop the line)

0

精彩评论

暂无评论...
验证码 换一张
取 消