如何在不带空格的字符串中转换Jsoup文档 [英] How to convert Jsoup Document in a String without put spaces
问题描述
我已经在Document对象Jsoup中转换了XML文档.原来,当我需要输出为String格式时,它会在下面生成此结果:
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>
...
生成的分数给我带来了很多问题,因为他在元素内使用了Colca空格,这给我带来了一个大问题.有什么方法可以在不更改元素值的情况下生成结果输出?我尝试过更改字符集并使用preetyprinter,但没有成功.
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.
如果commo在不修改元素内容的情况下生成以下示例,有没有办法做到这一点?
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>
...
输入
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.
推荐答案
通常,Jsoup将漂亮打印 xml读取的内容.您可以使用
Generally, Jsoup will pretty-print the read in xml. You can turn off that behavior with
doc.outputSettings().prettyPrint(false);
但是,那么JSoup可能会使用与输入相同的格式.就您而言,在< cUF>
标记周围可能还包含换行符,因此您不走运.
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.
我不确定您的原始xml的格式是否正确.但这可能会有所帮助:
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());
说明:我在解析之前删除了所有NEW LINE字符.然后我将漂亮的打印设置为关闭.
Explanation: I remove all NEW LINE characters before the parsing. Then I set the pretty print to off.
这篇关于如何在不带空格的字符串中转换Jsoup文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!