简体   繁体   English

如何从Java中的文本中获取整个XML

[英]How to fetch entire XML from text in Java

I have two different Strings for example: 我有两个不同的字符串例如:

1) 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) 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>

I need to parse these strings and fetch the entire XML element from root node without any headers. 我需要解析这些字符串,并从根节点获取没有任何标头的整个XML元素。 How do I achieve this? 我该如何实现?

  1. Remove the header - everything before 删除标题-之前的所有内容

<?xml <?xml

or 要么

<soap: <肥皂:

  1. Use some library for XML document parsing. 使用一些库进行XML文档解析。 For example, JAXP 例如, JAXP

If those are httpResponse objects you shouldn't have to modify them to read them. 如果这些是httpResponse对象,则无需修改它们即可读取它们。 The header and body are accessed separately. 标头和正文分别访问。

Here is an example (probably imperfect code, but it works): 这是一个示例(可能是不完美的代码,但是可以用):

        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(); 

This statement, "SOAPBody responseBody = response.getSOAPBody();" 此语句“ SOAPBody responseBody = response.getSOAPBody();” gets the soap body. 得到肥皂的身体。

If you are dealing with strings exactly as you reproduced then you'll need to use RegX to dispose of the bits that are not the XML body. 如果要完全按照复制的方式处理字符串,则需要使用RegX处置不是XML主体的位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM