Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从httppost响应解析xml_Java_Android_Xml_Http_Post - Fatal编程技术网

Java 从httppost响应解析xml

Java 从httppost响应解析xml,java,android,xml,http,post,Java,Android,Xml,Http,Post,在执行http POST期间,我将响应存储为字符串响应 HttpResponse httpresponse = httpclient.execute(httppost); HttpEntity resEntity = httpresponse.getEntity(); response = EntityUtils.toString(resEntity); 如果我打印响应,它看起来像: <?xml version="1.0" encoding="UTF-8"?> <respon

在执行http POST期间,我将响应存储为字符串响应

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>
还有各种类似的方法,但它不允许我运行代码,因为DocumentBuildFactory和InputSource无效


我应该如何从该XML中提取特定字符串?

1。使用
DOM解析器

例如:

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#####");
     }
DocumentBuilderFactory odbf=DocumentBuilderFactory.newInstance();
DocumentBuilder odb=odbf.newDocumentBuilder();
InputSource is=新的InputSource(新的StringReader(xml));
文档odoc=odb.parse(is);
odoc.getDocumentElement().normalize();//规范化文本表示
System.out.println(“文档的根元素是”+odoc.getDocumentElement().getNodeName());
NodeList LOP=odoc.getElementsByTagName(“位置”);
int totalPersons=LOP.getLength();
系统输出打印项次(“地点总数:“+总人数”);

对于(ints=0;s这只是一个快速而肮脏的测试。它对我很有效

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) {
        }
    }
}
import java.io.IOException;
导入java.io.StringReader;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.w3c.dom.Document;
导入org.w3c.dom.NodeList;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
公开课考试{
公共静态void main(字符串[]args){
字符串xml=“lo8mdn7bientr71b5kn1kote90”;
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
文档生成器;
输入源为;
试一试{
builder=factory.newDocumentBuilder();
is=新输入源(新StringReader(xml));
文档doc=builder.parse(is);
NodeList list=doc.getElementsByTagName(“sessionID”);
System.out.println(list.item(0.getTextContent());
}捕获(ParserConfiguration异常e){
}捕获(SAXE异常){
}捕获(IOE异常){
}
}
}
输出
lo8mdn7bientr71b5kn1kote90

感谢您的响应,这确实有效唯一的问题是当我使用实际响应时,它不会起作用。我认为它起作用是因为字符串包含未正确转义的引号。我不确定您在响应中得到的确切内容,有一种猜测是它可能包含BOM(有关详细信息,请使用google XML BOM)在UTF字符串的开头。如果您有一个调试器(如Eclipse中的调试器),那么应该非常直接地找出它与硬编码工作字符串之间的区别。如果响应的文本编码是UTF-8,那么可能还值得尝试更改response=EntityUtils.toString(resEntity);to response=EntityUtils.toString(resEntity,UTF-8);对我来说,这是处理此类响应的最佳方法之一
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) {
        }
    }
}