Wilson Ribeiro
Wilson Ribeiro

Reputation: 373

How to convert Jsoup Document in a String without put spaces

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.

Upvotes: 0

Views: 1730

Answers (1)

luksch
luksch

Reputation: 11712

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.

Upvotes: 1

Related Questions