Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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
Android XML解析错误_Android_Xml - Fatal编程技术网

Android XML解析错误

Android XML解析错误,android,xml,Android,Xml,我正在使用一个web服务,因为它的输出是XML字符串。我正在将其解析为一个文档,并逐个获取每个子节点的值,然后插入到我的SQLlite表中,如下所示: public void DownloadAndInsertProblemAndReasonCode(String serverIPAddress, String deviceId) { String SOAP_ACTION = "http://VisionEPODWebService/GetProblemA

我正在使用一个web服务,因为它的输出是XML字符串。我正在将其解析为一个文档,并逐个获取每个子节点的值,然后插入到我的SQLlite表中,如下所示:

public void DownloadAndInsertProblemAndReasonCode(String serverIPAddress,
            String deviceId) {
        String SOAP_ACTION = "http://VisionEPODWebService/GetProblemAndReasonCodesNew";
        String OPERATION_NAME = "GetProblemAndReasonCodesNew";
        String WSDL_TARGET_NAMESPACE = "http://VisionEPODWebService/";
        String SOAP_ADDRESS = "";

        SOAP_ADDRESS = "http://" + serverIPAddress
                + "/VisionEPODWebService/SystemData.asmx";
        SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER10);
        request.addProperty("deviceID", deviceId);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        try {
            httpTransport.call(SOAP_ACTION, envelope);
            Object response = envelope.getResponse();
            DocumentBuilderFactory docBuilberFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder docBuilder = docBuilberFactory.newDocumentBuilder();
            InputSource inputSource = new InputSource();
            inputSource
                    .setCharacterStream(new StringReader(response.toString()));
            Document doc = docBuilder.parse(inputSource);
            NodeList nodesProblemCode = doc
                    .getElementsByTagName("tblProblemCode");
            ContentValues initialProblemCodeValues = new ContentValues();
            dbAdapter = new DatabaseAdapter(this.context);
            dbAdapter.open();
            dbAdapter.BeginTransaction();
            dbAdapter.DeleteRecord("tblProblemCode", "", "");

            for (int i = 0; i < nodesProblemCode.getLength(); i++) {
                Element element = (Element) nodesProblemCode.item(i);

                NodeList PKProblemCode = element
                        .getElementsByTagName("PKProblemCode");
                Element line = (Element) PKProblemCode.item(0);
                initialProblemCodeValues.put("PKProblemCode",
                        getCharacterDataFromElement(line).toString());

                NodeList ProblemCode = element
                        .getElementsByTagName("ProblemCode");
                line = (Element) ProblemCode.item(0);
                initialProblemCodeValues.put("ProblemCode",
                        getCharacterDataFromElement(line));

                NodeList ProblemCodeDescription = element
                        .getElementsByTagName("ProblemCodeDescription");
                line = (Element) ProblemCodeDescription.item(0);
                initialProblemCodeValues.put("ProblemCodeDescription",
                        getCharacterDataFromElement(line).toString());

                NodeList VWReturn = element.getElementsByTagName("VWReturn");
                line = (Element) VWReturn.item(0);
                initialProblemCodeValues.put("VWReturn",
                        getCharacterDataFromElement(line).toString());

                dbAdapter.InsertRecord("tblProblemCode", "",
                        initialProblemCodeValues);
            }

            NodeList nodesReasonCode = doc
                    .getElementsByTagName("tblReasonCode");
            ContentValues initialReasonCodeValues = new ContentValues();
            dbAdapter.DeleteRecord("tblReasonCode", "", "");
            for (int i = 0; i < nodesReasonCode.getLength(); i++) {
                Element element = (Element) nodesReasonCode.item(i);

                NodeList PKReasonCode = element
                        .getElementsByTagName("PKReasonCode");
                Element line = (Element) PKReasonCode.item(0);
                initialReasonCodeValues.put("PKReasonCode",
                        getCharacterDataFromElement(line).toString());

                NodeList ReasonDescription = element
                        .getElementsByTagName("ReasonDescription");
                line = (Element) ReasonDescription.item(0);
                initialReasonCodeValues.put("ReasonDescription",
                        getCharacterDataFromElement(line));

                dbAdapter.InsertRecord("tblReasonCode", "",
                        initialReasonCodeValues);
            }
            dbAdapter.SetSucessfulTransaction();
            dbAdapter.EndTransaction();
            dbAdapter.close();
        }

        catch (Exception exception) {
            exception.toString();
        }

    }


public static String getCharacterDataFromElement(Element e) {

        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
        return "?";
    }
public void downloads和insertproblemandreasoncode(字符串serverIPAddress,
字符串设备ID){
字符串SOAP_ACTION=”http://VisionEPODWebService/GetProblemAndReasonCodesNew";
字符串操作\u NAME=“GetProblemandreasonCodeNew”;
字符串WSDL_目标_命名空间=”http://VisionEPODWebService/";
字符串SOAP_ADDRESS=“”;
SOAP\u ADDRESS=“http://”+服务器IP地址
+“/VisionEPODWebService/SystemData.asmx”;
SoapObject请求=新的SoapObject(WSDL\u目标\u命名空间,
操作名称);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(
第10版);
请求.addProperty(“deviceID”,deviceID);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
HttpTransportSE httpTransport=新的HttpTransportSE(SOAP\U地址);
试一试{
调用(SOAP_操作,信封);
对象响应=envelope.getResponse();
DocumentBuilderFactory docbuilderFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder=docBuilberFactory.newDocumentBuilder();
InputSource InputSource=新的InputSource();
输入源
.setCharacterStream(新的StringReader(response.toString());
Document doc=docBuilder.parse(inputSource);
NodeList nodesProblemCode=doc
.getElementsByTagName(“tblProblemCode”);
ContentValues initialProblemCodeValues=新ContentValues();
dbAdapter=新数据库适配器(this.context);
dbAdapter.open();
dbAdapter.BeginTransaction();
dbAdapter.DeleteRecord(“tblProblemCode”,“参数”,“参数”);
对于(int i=0;i
问题是,一段时间后,XML字符串将不再具有某些子节点。例如 “ProblemCode”子节点…此时代码显示错误


如何检查文档中是否存在元素,或者元素的值是否存在?

我认为您需要检查子节点是否存在:特别是针对您的问题节点:

NodeList NL = element.getElementsByTagName("PKProblemCode");
if ((NL==null)||(NL.getLength()==0)) {
  //No such node so use a default instead
} else 
  Element line = (Element) PKProblemCode.item(0);

我认为您需要检查子节点是否存在:特别是针对您的问题节点:

NodeList NL = element.getElementsByTagName("PKProblemCode");
if ((NL==null)||(NL.getLength()==0)) {
  //No such node so use a default instead
} else 
  Element line = (Element) PKProblemCode.item(0);