简体   繁体   English

如何删除 XML 文件中的 LF 以接收一行 xml 文件?

[英]How to delete LF in XML-Files to receive one line xml file?

I have a file theme.xml that contains no "LF" see https://en.wikipedia.org/wiki/Newline .我有一个不包含“LF”的文件 theme.xml 见https://en.wikipedia.org/wiki/Newline But after converting to js and return to xml my result contains "LF" line breaker.但是在转换为 js 并返回到 xml 后,我的结果包含“LF”换行符。 Is is a problem, because adobe acrobat 8 standard requires the xml file in one line as a header template file.是有问题,因为adobe acrobat 8​​标准要求一行中的xml文件作为头模板文件。 hear an example:听一个例子:

<?xml version = "1.0" encoding = "UTF-8" ?><HeaderFooterSettings version = "8.0"><Font name="Arial" size="11.0"/><Color b="0.0" r="0.0" g="0.0"/><Margin top="34.0159" left="72.0002" right="72.0002" bottom="36.0001"/><Appearance shrink="1" fixedprint="0"/><PageRange end="-1" start="-1" even="1" odd="1"/><Page offset = "0"><PageIndex format="1"/></Page><Date><Month format="1"/>/<Day format="1"/><Year format="0"/></Date><Header><Left>hello</Left><Center></Center><Right>world</Right></Header><Footer><Left></Left><Center></Center><Right></Right></Footer></HeaderFooterSettings>






var fs = require('fs');
var path = require('path');
var xml2js = require('xml2js');

xmlFileToJs('theme.xml', function (err, obj) {
    if (err) throw (err);
    jsToXmlFile('theme2.xml', obj, function (err) {
        if (err) console.log(err);
    })
});


function xmlFileToJs(filename, cb) {
    var filepath = path.normalize(path.join(__dirname, filename));
    fs.readFile(filepath, 'utf8', function (err, xmlStr) {
        if (err) throw (err);
        xml2js.parseString(xmlStr, {}, cb);
    });    
}

function jsToXmlFile(filename, obj, cb) {
    var filepath = path.normalize(path.join(__dirname, filename));
    var builder = new xml2js.Builder();
    var xml = builder.buildObject(obj);
    fs.writeFile(filepath, xml, cb);
}

and results unfortunatly with linebrakers "LF"结果不幸的是线闸“LF”

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<HeaderFooterSettings version="8.0">
  <Font name="Arial" size="11.0"/>
  <Color b="0.0" r="0.0" g="0.0"/>
  <Margin top="34.0159" left="72.0002" right="72.0002" bottom="36.0001"/>
  <Appearance shrink="1" fixedprint="0"/>
  <PageRange end="-1" start="-1" even="1" odd="1"/>
  <Page offset="0">
    <PageIndex format="1"/>
  </Page>
  <Date>
    /
    <Month format="1"/>
    <Day format="1"/>
    <Year format="0"/>
  </Date>
  <Header>
    <Left>hello</Left>
    <Center/>
    <Right>world</Right>
  </Header>
  <Footer>
    <Left/>
    <Center/>
    <Right/>
  </Footer>
</HeaderFooterSettings>

I tried a lot.我尝试了很多。 for instant, but cannot find a solution by this commands:即时,但无法通过此命令找到解决方案:

const stringify = require('xml-stringify');
var xmlc = require('xml');
const trimNewlines = require('trim-newlines');
xmlc(trimNewlines(JSON.stringify(obj)));

also I tried this:我也试过这个:

var builder = new xml2js.Builder({'trim' : true, 'normalize' : true});

well no chance.好吧,没机会了。

I look forward for other ideas.我期待其他想法。

现在你可以使用

const builder = new xml2js.Builder({ renderOpts: { pretty: false } });
function onelinexml (xml) {

        var pos = xml.search(" <");
        var xmlstr = xml.replace(/\n|  |   /g, ""); 
        console.log('pos=%d',pos);
        while (pos>=0){

        var xmlstr = xmlstr.replace(/\n|  |   /g, "");  
        var xmlstr = xmlstr.replace(/> /g, ">");
        var xmlstr = xmlstr.replace(/ </g, "<");
        pos = xmlstr.search(" <");
        console.log('pos=%d',pos);
        }
        return xmlstr;
}

solved.解决了。 Thx.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM