jsoup是一款Java的HTML解析器,主要用来对HTML解析,也是可以用于解析XML的,一些注意事项如下。
XML文本转换成Document,设置Parser为xmlParser
Document document = Jsoup.parse(body, "", Parser.xmlParser());
操作完Document之后,想要转换成String,直接采用toString()方式,xml的标签总是换行,很不美观,类似这样:
<soapenv:header>
<v1:header>
<v1:msgtype>
REQUEST
</v1:msgtype>
<v1:communicationpattern>
SimpleResponse
</v1:communicationpattern>
<v1:communicationstyle>
RPC
</v1:communicationstyle>
</v1:header>
</soapenv:header>
加一行设置即可
document.outputSettings().prettyPrint(false);
document.toString();
<soapenv:Header>
<v1:header>
<v1:msgtype>REQUEST</v1:msgtype>
<v1:communicationPattern>SimpleResponse</v1:communicationPattern>
<v1:communicationStyle>RPC</v1:communicationStyle>
</v1:header>
</soapenv:Header>