从httppost响应中解析xml
执行http POST期间,我将响应存储为字符串响应。从httppost响应中解析xml
HttpResponse httpresponse = httpclient.execute(httppost);
HttpEntity resEntity = httpresponse.getEntity();
response = EntityUtils.toString(resEntity);
如果我打印响应,它看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<response status="ok">
<sessionID>lo8mdn7bientr71b5kn1kote90</sessionID>
</response>
我想刚才的SessionID存储为一个字符串。我试过
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
各种方法这样,但它不会让我跑的代码,因为DocumentBuildFactory和InputSource的是无效的。
我应该怎样从XML中提取特定的字符串?
这只是快速和肮脏的测试。它为我工作。
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class Test {
public static void main(String[] args) {
String xml= "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response status=\"ok\"><sessionID>lo8mdn7bientr71b5kn1kote90</sessionID></response>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
InputSource is;
try {
builder = factory.newDocumentBuilder();
is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("sessionID");
System.out.println(list.item(0).getTextContent());
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
}
}
输出: lo8mdn7bientr71b5kn1kote90
感谢的最好方式之一响应,这确实是唯一的问题,当我使用我的实际响应它没有。我认为这是因为该字符串包含不正确转义的引号。 – Ted 2012-08-15 17:30:12
我不确定你在响应中究竟得到了什么,有人猜测它可能在UTF字符串的开始处包含BOM(更多信息的Google XML BOM)。如果你有一个调试器(比如Eclipse中的调试器),应该非常直截了当地找出它和硬编码工作字符串之间的区别。 – 2012-08-15 18:54:16
如果响应的文本编码是UTF-8,也许它也值得尝试改变response = EntityUtils.toString(resEntity); to response = EntityUtils.toString(resEntity,“UTF-8”); – 2012-08-15 18:59:33
1.使用DOM Parser
。
如:
DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance();
DocumentBuilder odb = odbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document odoc = odb.parse(is);
odoc.getDocumentElement().normalize(); // normalize text representation
System.out.println ("Root element of the doc is " + odoc.getDocumentElement().getNodeName());
NodeList LOP = odoc.getElementsByTagName("locations");
int totalPersons =LOP.getLength();
System.out.println("Total nos of locations:"+totalPersons);
for(int s=0; s<LOP.getLength() ; s++)
{
Node FPN =LOP.item(s);
if(FPN.getNodeType() == Node.ELEMENT_NODE)
{
Element latlon = (Element)FPN;
NodeList oNameList1 = latlon.getElementsByTagName("featured");
Element firstNameElement = (Element)oNameList1.item(0);
NodeList textNList1 = firstNameElement.getChildNodes();
//this.setLocationId(((Node)textNList1.item(0)).getNodeValue().trim());
featuredArr = changeToBoolean(((Node)textNList1.item(0)).getNodeValue().trim()); // value taken
System.out.println("#####The Parsed data#####");
System.out.println("featured : " + ((Node)textNList1.item(0)).getNodeValue().trim());
System.out.println("#####The Parsed data#####");
}
请参阅此链接了解详情:
我的[KSOAP2(http://ksoap2.sourceforge.net/)是处理这种反应 – mihail 2012-08-15 14:55:18