【问题标题】:Extract only XML from a string response从字符串响应中仅提取 XML
【发布时间】:2014-08-30 17:11:03
【问题描述】:

我收到了一个 XML 响应以及 HTTP 响应中的调用堆栈值。我成功地解析了 XML。但是在先提取 XML 时遇到了麻烦。下面是我要提取的 XML。

 <?xml version="1.0" encoding="UTF-8"?> <response
 status="ok"><token>af53bd59811111</token></response>

来自以下字符串,它是一个 httpresponse。我尝试了一下字符串 RegEx,无法继续。

Notice: Undefined index: Content-type in /data/home/web/src/api_API_Stage_381_201404171313/API/Service/File/Abstract.php on line 251

Call Stack:
    0.0001     645184   1. {main}() /data/home/web/src/api_API_Stage_381_201404171313/www/index.php:0
    0.0374    7276184   2. Zend_Controller_Front->dispatch() /data/home/web/src/api_API_Stage_381_201404171313/www/index.php:10
    0.0426    8577512   3. Zend_Controller_Dispatcher_Standard->dispatch() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Controller/Front.php:946
    0.0454    8972072   4. Zend_Controller_Action->dispatch() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Controller/Dispatcher/Standard.php:289
    0.0454    8977384   5. FileController->indexAction() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Controller/Action.php:513
    0.0526   10124216   6. Zend_Rest_Server->handle() /data/home/web/src/api_API_Stage_381_201404171313/controllers/index/FileController.php:41
    0.0527   10130800   7. Zend_Rest_Server->_callObjectMethod() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Rest/Server.php:228
    0.0528   10131864   8. Zend_Server_Reflection_Method->invokeArgs() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Rest/Server.php:603
    0.0528   10132304   9. Zend_Server_Reflection_Function_Abstract->__call() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Rest/Server.php:603
    0.0528   10132744  10. call_user_func_array() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Server/Reflection/Function/Abstract.php:368
    0.0528   10133272  11. API_Service_File->address() /data/home/web/src/ZendFramework-1.9.5-modified/library/Zend/Server/Reflection/Function/Abstract.php:0
    0.0705   12262480  12. API_Service_File_Abstract->handle() /data/home/web/src/api_API_Stage_381_201404171313/API/Service/File.php:30
    0.0705   12262480  13. API_Service_File_Address->post() /data/home/web/src/api_API_Stage_381_201404171313/API/Service/File/Abstract.php:52
    0.0705   12262480  14. API_Service_File_Abstract->_post() /data/home/web/src/api_API_Stage_381_201404171313/API/Service/File/Address.php:61
    0.0705   12262696  15. API_Service_File_Abstract->_rawUpload() /data/home/web/src/api_API_Stage_381_201404171313/API/Service/File/Abstract.php:147

<?xml version="1.0" encoding="UTF-8"?>
<response status="ok"><token>af53bd59811111</token></response>

XML 被解析并被下面的 XML 解析器捕获到了令牌

            responseXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <response status=\"ok\"><token>af53bd598f5a411</token></response>";
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        InputSource inputsource;

        try {
            builder = factory.newDocumentBuilder();
            inputsource = new InputSource(new StringReader(responseXml));
            Document doc = builder.parse(inputsource);
            NodeList list = doc.getElementsByTagName("token");
            System.out.println("Got Token!! -" + list.item(0).getTextContent());
        }catch (ParserConfigurationException e) {
        }

【问题讨论】:

    标签: java xml regex string parsing


    【解决方案1】:

    您可以使用 String.indexOf(String) 方法在 HttpResponse 中定位 XML 的起始位置,然后使用 String.substring(int) 提取 XML,例如:

        String responseXml = response.substring(response.indexOf("<?xml"));
    

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 2018-06-11
      • 2013-08-15
      • 1970-01-01
      • 2013-05-02
      • 2013-07-22
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多