如何从 Java 中的文本中获取整个 XML
How to fetch entire XML from text in Java
我有两个不同的字符串,例如:
1)
HTTP/1.1 200 OK
Content-Length: 144
content-type: application/xml
Date: Wed, 11 Nov 2015 12:57:07 GMT
Server: Jetty(7.5.4.v20111024)
Note: Header order may not reflect actual transmitted stream.
<?xml version="1.0" encoding="UTF-8"?>
<Log>
<Code>0</Code>
<SensitiveData2>Data Is Here</SensitiveData2>
</Log>
2)
HTTP/1.1 200 OK
Content-Type: text/xml;charset=UTF-8
Content-Length: 1099
Server: Jetty(7.5.4.v20111024)
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Response_Phase_IIResponse xmlns="http://tempuri.org/">
<Response_Phase_IIResult>
<Address_Parameters_Phase_II>
<Setup_In_CRM>NO</Setup_In_CRM>
<CRM_Profile_ID>0</CRM_Profile_ID>
</Address_Parameters_Phase_II>
</Response_Phase_IIResult>
</Response_Phase_IIResponse>
</soap:Body>
</soap:Envelope>
我需要解析这些字符串并从根节点获取整个 XML 元素,而不需要任何 headers。我该如何实现?
- 删除 header -
之前的所有内容
<?xml
或
<soap:
- 使用一些库进行 XML 文档解析。例如,JAXP
如果这些是 httpResponse objects,您不必修改它们就可以阅读它们。 header 和 body 是分开访问的。
这是一个示例(可能是不完美的代码,但它有效):
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
URL endpoint = null;
try {
endpoint =
new URL(new URL(url), path, new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
URL target = new URL(url.toString());
URLConnection connection = target.openConnection();
// Connection settings
connection.setConnectTimeout(10000); // 10 sec
connection.setReadTimeout(30000); // 1 min
return(connection);
}
});
} catch (Exception e) {
throw new SOAPException("Connection unavailable");
}
SOAPMessage response = null;
try {
response = connection.call(message, endpoint);
} catch (SOAPException e) {
connection.close();
throw new SOAPException(e);
}
try {
SOAPBody responseBody = response.getSOAPBody();
if(responseBody.getFault()!=null){
throw new SOAPException("Message unreadable");
}
} catch (SOAPException e) {
connection.close();
throw new SOAPException(e);
}
connection.close();
这条语句,"SOAPBody responseBody = response.getSOAPBody();"得到肥皂body。
如果您处理的字符串与您复制的完全一样,那么您将需要使用 RegX 来处理不是 XML body.
的位
我有两个不同的字符串,例如:
1)
HTTP/1.1 200 OK
Content-Length: 144
content-type: application/xml
Date: Wed, 11 Nov 2015 12:57:07 GMT
Server: Jetty(7.5.4.v20111024)
Note: Header order may not reflect actual transmitted stream.
<?xml version="1.0" encoding="UTF-8"?>
<Log>
<Code>0</Code>
<SensitiveData2>Data Is Here</SensitiveData2>
</Log>
2)
HTTP/1.1 200 OK
Content-Type: text/xml;charset=UTF-8
Content-Length: 1099
Server: Jetty(7.5.4.v20111024)
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Response_Phase_IIResponse xmlns="http://tempuri.org/">
<Response_Phase_IIResult>
<Address_Parameters_Phase_II>
<Setup_In_CRM>NO</Setup_In_CRM>
<CRM_Profile_ID>0</CRM_Profile_ID>
</Address_Parameters_Phase_II>
</Response_Phase_IIResult>
</Response_Phase_IIResponse>
</soap:Body>
</soap:Envelope>
我需要解析这些字符串并从根节点获取整个 XML 元素,而不需要任何 headers。我该如何实现?
- 删除 header - 之前的所有内容
<?xml
或
<soap:
- 使用一些库进行 XML 文档解析。例如,JAXP
如果这些是 httpResponse objects,您不必修改它们就可以阅读它们。 header 和 body 是分开访问的。
这是一个示例(可能是不完美的代码,但它有效):
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
URL endpoint = null;
try {
endpoint =
new URL(new URL(url), path, new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL url) throws IOException {
URL target = new URL(url.toString());
URLConnection connection = target.openConnection();
// Connection settings
connection.setConnectTimeout(10000); // 10 sec
connection.setReadTimeout(30000); // 1 min
return(connection);
}
});
} catch (Exception e) {
throw new SOAPException("Connection unavailable");
}
SOAPMessage response = null;
try {
response = connection.call(message, endpoint);
} catch (SOAPException e) {
connection.close();
throw new SOAPException(e);
}
try {
SOAPBody responseBody = response.getSOAPBody();
if(responseBody.getFault()!=null){
throw new SOAPException("Message unreadable");
}
} catch (SOAPException e) {
connection.close();
throw new SOAPException(e);
}
connection.close();
这条语句,"SOAPBody responseBody = response.getSOAPBody();"得到肥皂body。
如果您处理的字符串与您复制的完全一样,那么您将需要使用 RegX 来处理不是 XML body.
的位