How to convert Jsoup Document in a String without put spaces

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-01 09:24:33

问题


I have converted an XML document within a Document object Jsoup. Turns out, when I need to output to String format it generates this result below:

<?xml version="1.0" standalone="yes"?>
<NewDataSet xmlns="http://www.portalfiscal.inf.br/nfe"> 
 <nfeProc versao="2.00">
  <NFe> 
   <infNFe versao="2.00" id="NFe31140545453214002014550120002685744002685742"> 
 <cUF>
   31
 </cUF> 
 <cNF>
  00268574
 </cNF>
...

Scores generated this brings me a lot of problems, since he Colca whitespace within elements, and this causes me a big problem. Is there any way to generate an output that result without changing the values ​​of the elements? I've tried changing the charset and use preetyprinter, but without success.

If commo generate the example below, without modifying the contents of the elements, there is a way to do this?

<?xml version="1.0" standalone="yes"?>
<NewDataSet xmlns="http://www.portalfiscal.inf.br/nfe"> 
 <nfeProc versao="2.00">
  <NFe> 
   <infNFe versao="2.00" id="NFe31140545453214002014550120002685744002685742"> 
 <cUF>31</cUF> 
 <cNF>00268574</cNF>
...

EDIT: input

String xml = "";

        while (reader.ready()) {
            xml += reader.readLine();
        }
        reader.close();
        doc = Jsoup.parse(xml, "", Parser.xmlParser());

output: I tried various ways, but always the same result as above...

 doc.toString();
 doc.outerHtml();
 doc.Html();

tried all methods that return a string, but always return the same.


回答1:


Generally, Jsoup will pretty-print the read in xml. You can turn off that behavior with

doc.outputSettings().prettyPrint(false);

However, then JSoup will probably use the same formatting as the input. In your case that contains maybe also new line characters around the <cUF> tags, so you are out of luck there.

I am not sure how your original xml is formatted really. But maybe this can be of help:

while (reader.ready()) {
    xml += reader.readLine().replaceAll("\n","");
}
reader.close();
doc = Jsoup.parse(xml, "", Parser.xmlParser());
doc.outputSettings().prettyPrint(false).indentAmount(0);

System.out.print(doc.html());

Explanation: I remove all NEW LINE characters before the parsing. Then I set the pretty print to off.



来源:https://stackoverflow.com/questions/24088347/how-to-convert-jsoup-document-in-a-string-without-put-spaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!