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>