我在HTTP响应中获得了XML响应以及调用堆栈值。我成功地解析了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) {
}
答案 0 :(得分:0)
您可以使用String.indexOf(String)方法在HttpResponse中找到XML的起始位置,然后使用String.substring(int)来提取XML,例如:
String responseXml = response.substring(response.indexOf("<?xml"));