简体   繁体   English

无法正确解析xml中的数据

[英]Not getting data in xml parsing properly

Here I have no data, Then I got Pcode 0, but here i could not get Pcode 0, My intention is getting Pcode zero and display "Not Data Here" Message. 在这里我没有数据,然后我得到了Pcode 0,但是在这里我没有得到Pcode 0,我的意图是让Pcode 0并显示“ Not Data Here”消息。 I have using data parsing with xml, but could not properly, I have error with parsing error, but could not get find, where i am doing error. 我使用xml进行数据解析,但无法正确执行,解析错误,但无法找到错误,我在哪里出错。 Here is the my code: 这是我的代码:

 Log.d("user id is ", userId + "!");                

 String input = String.format("<Es_Request><Data><UserId>%s</UserId></Data></Es_Request>",  userId);
 String response = HttpPostRetreiver .retriver(Url.url_getting_assignment,                                      input, GradeAssignmentActivity.this);
        if (response != null) {
    esResponse = XmlParser.parseAssignmentProfessorResponse(response);
        Log.d("TAG", "Checking Value="+ esResponse.getResponseCode().getpCode());
        if (esResponse.getResponseCode().getpCode().equalsIgnoreCase("1")) {
        assignmentDataInfo = esResponse .getAssignmentListData().getAssignmentDataInfo();
        int size = assignmentDataInfo.size();
        int i = 0;
        for (; i < size; i++) {
                EduInterface.assignmetIdArrayList.add(assignmentDataInfo.get(i).getAssignmentId());
                EduInterface.assignmetNameArrayList.add(assignmentDataInfo.get(i).getAssignmentName());
      }
    } else {

       test = 1;
       Log.d("else", "Not data Here");
  }
    }

  } else {
    Log.d("TAG", "Error in Webservice");

    }
    } catch (Exception e) {
        Log.e("Grade Assignment class", e.toString() + "");
 }

I Have got,First user id is 5 ! 我有,第一个用户ID是5! then got error Grade Assignemt Class error. 然后出现错误Grade Assignemt Class错误。 I have been problem parsing Here is the pasing> 我一直在解析问题,这是粘贴>

Input String: Es_Request><Data><UserId>913</UserId></Data></Es_Request> 输入字符串: Es_Request><Data><UserId>913</UserId></Data></Es_Request>

Response String : 响应字符串:

<?xml version="1.0" encoding="utf-8"?><Es_Response><Es_Session_Id></Es_Session_Id>
 <Bpn_Request_Id></Bpn_Request_Id><Response_Code><Pcode>0</Pcode><Scode>Unsuccessful</Scode></Response_Code><Data><message>Assignments not found for Professor & CourseId.</message></Data></Es_Response>

Here is the my handler class: 这是我的处理程序类:

@Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        content = new StringBuilder();
        if (localName.equalsIgnoreCase("Data")) {
            assignmentListData=new AssignmentListData();
        } else if (localName.equalsIgnoreCase("assignments")) {
            assignmentItems = new AssignmentItems();
        }
        else if (localName.equalsIgnoreCase("Es_Response")) {
            esResponse=new EsResponse();
        } else if (localName.equalsIgnoreCase("Response_Code")) {
            responseCode=new ResponseCode();
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
         if (localName.equalsIgnoreCase("Pcode")) {
              responseCode.setpCode(content.toString());
              Log.d("TAG", "Pcode Value->"+content.toString());
            }
          else if (localName.equalsIgnoreCase("Response_Code")) {
              esResponse.setResponseCode(responseCode);
            }
          else if (localName.equalsIgnoreCase("AssignmentId")) {
              assignmentItems.setAssignmentId(content.toString());
            }
          else if (localName.equalsIgnoreCase("AssignmentName")) {
              assignmentItems.setAssignmentName(content.toString());
            }

          else if (localName.equalsIgnoreCase("Assignments")) {
              assignmentDataInfo.add(assignmentItems);
            }
          else if (localName.equalsIgnoreCase("Data")) {
              assignmentListData.setAssignmentDataInfo(assignmentDataInfo);
            }
          else if (localName.equalsIgnoreCase("Es_Response")) {
                esResponse.setAssignmentListData(assignmentListData);
            }

    }

I got logcat this: 我得到logcat这个:

04-30 19:25:51.378: D/getting course list->(1598): Line 322---RESPONSE<?xml version="1.0" encoding="utf-8"?><Es_Response><Es_Session_Id></Es_Session_Id>
04-30 19:25:51.378: D/getting course list->(1598): <Bpn_Request_Id></Bpn_Request_Id><userIDName>913</userIDName><schoolID>1</schoolID><CourseID>977</CourseID><Response_Code><Pcode>0</Pcode><Scode>Unsuccessful</Scode><Test>977</Test><ROLE ID>4</ROLE ID></Response_Code><Data><message>Assignments not found for Professor & CourseId.</message></Data></Es_Response>
04-30 19:25:51.378: D/user id is(1598): 913
04-30 19:25:51.378: D/school id is(1598): 1
04-30 19:25:51.388: D/TAG(1598): Pcode Value->0
04-30 19:25:51.398: W/System.err(1598): org.apache.harmony.xml.ExpatParser$ParseException: At line 2, column 189: not well-formed (invalid token)
04-30 19:25:51.398: W/System.err(1598):     at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:499)
04-30 19:25:51.398: W/System.err(1598):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:484)
04-30 19:25:51.409: W/System.err(1598):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:309)
04-30 19:25:51.409: W/System.err(1598):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:267)
04-30 19:25:51.409: W/System.err(1598):     at sdei.edustatusnew.services.XmlParser.parseAssignmentProfessorResponse(XmlParser.java:840)
04-30 19:25:51.409: W/System.err(1598):     at sdei.edustatusnew.gradebook.GradeAssignmentActivity$GetDataTask.doInBackground(GradeAssignmentActivity.java:326)
04-30 19:25:51.409: W/System.err(1598):     at sdei.edustatusnew.gradebook.GradeAssignmentActivity$GetDataTask.doInBackground(GradeAssignmentActivity.java:1)
04-30 19:25:51.427: W/Trace(1598): Unexpected value from nativeGetEnabledTags: 0
04-30 19:25:51.427: W/Trace(1598): Unexpected value from nativeGetEnabledTags: 0
04-30 19:25:51.438: W/Trace(1598): Unexpected value from nativeGetEnabledTags: 0
04-30 19:25:51.438: W/Trace(1598): Unexpected value from nativeGetEnabledTags: 0
04-30 19:25:51.468: W/System.err(1598):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-30 19:25:51.478: W/System.err(1598):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-30 19:25:51.478: W/System.err(1598):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-30 19:25:51.488: W/System.err(1598):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-30 19:25:51.488: W/System.err(1598):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-30 19:25:51.488: W/System.err(1598):     at java.lang.Thread.run(Thread.java:856)
04-30 19:25:51.488: E/Grade Assignment class(1598): java.lang.NullPointerException

Would you please help, What is the error on my code. 请您帮忙,我的代码有什么错误。 Thanks In Advance. 提前致谢。

You have a error in your XML. 您的XML错误。 It is crystal clear in your logcat stacktrace ie, org.apache.harmony.xml.ExpatParser$ParseException: At line 2, column 189: not well-formed (invalid token) 在您的logcat堆栈跟踪中非常清晰,即org.apache.harmony.xml.ExpatParser$ParseException: At line 2, column 189: not well-formed (invalid token)

The tag at line 2 column 189 is <ROLE ID>4</ROLE ID> . 第2行第189列的标记是<ROLE ID>4</ROLE ID> The XML tag should not contain any white space characters. XML标签不应包含任何空格字符。

The solution is change it to <ROLE_ID>4</ROLE_ID> 解决方案是将其更改为<ROLE_ID>4</ROLE_ID>

I think, you have been problem in Assignments not found for Professor & CourseId. 我认为,您在“教授与课程ID”找不到的作业中遇到了问题 tag in response string. 在响应字符串中标记。 Please try &-> and you may solve this. 请尝试&->,您可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM