Underscore-java有静态方法U.xmlToJson(xml)
。活生生的例子
import com.github.underscore.U;
public class Main {
public static void main(String args[]) {
String xml = "<s:Envelope \n"
+ "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><Response \n"
+ "xmlns=\"http://tempuri.org/\"><UserResult><Users xmlns=\"\"><User>\n"
+ "<Message>Success</Message>\n"
+ "<UserId>213213213</UserId>\n"
+ "<FullName>Abc</FullName>\n"
+ "<Roles>\n"
+ "<Role>\n"
+ "<RoleId>23232333</RoleId>\n"
+ "<RoleName>Salesperson</RoleName>\n"
+ "</Role>\n"
+ "</Roles>\n"
+ "</User>\n"
+ "</Users>\n"
+ "</UserResult></Response>\n"
+ "</s:Body>\n"
+ "</s:Envelope>";
System.out.println(U.xmlToJson(xml));
}
}
输出:
{
"s:Envelope": {
"-xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/",
"s:Body": {
"Response": {
"-xmlns": "http://tempuri.org/",
"UserResult": {
"Users": {
"-xmlns": "",
"User": {
"Message": "Success",
"UserId": "213213213",
"FullName": "Abc",
"Roles": {
"Role": {
"RoleId": "23232333",
"RoleName": "Salesperson"
}
}
}
}
}
}
}
},
"#omit-xml-declaration": "yes"
}