簡體   English   中英

XML解析,用於在登錄時獲取錯誤值

[英]XML Parsing for getting error values while logging in

以下是我的xml和Java文件,當我嘗試查找錯誤標簽值時,得到的響應為

<?xml version="1.0" encoding="UTF-8" ?>
<Error>Invalid User Id/Password. Please try again</Error> 

這是我的XML

<?xml version="1.0" encoding="UTF-8" ?>  
<MENU-ITEMS>
<project>XYX</project>
<project_name>XYZ DEMO TESTING</project_name>
<curr_stu_id>ABC-2222</curr_stu_id>  
<curr_stu_name>P.E. Joseph</curr_stu_name>
</MENU-ITEMS>

這是我的Java FileJava文件:-

 public void onResponse(String response)  
    {  
    Log.d(TAG, "Login response: " + response);  
    if (response.contains("<Error>"))  
    {  
    String[] responseLines = response.split("\n");  
    String message = responseLines[2].replace("<Error>","").replace("</Error>","");  
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  
    }  
    else  
    {  
    startActivity(new Intent(MainActivity.this, Welcome.class)  
    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)  
    .putExtra("response",response)  
    .putExtra("userName", username.getText().toString()));  
    }

   // I want only the tag value to appear when I am logging in, please help me. I do not want to use  
    String[] responseLines = response.split("\n");  
    String message = responseLines[2].replace("<Error>","").replace("</Error>","");  
    Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  

添加一個類XMLParser:

package com.yourpackagename;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML DOM element
     * @param xml string
     * */
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

    /** Getting node value
      * @param elem element
      */
     public final String getElementValue(Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }

     /**
      * Getting node value
      * @param  item Element
      * @param str String
      * */
     public String getValue(Element item, String str) {
            NodeList n = item.getElementsByTagName(str);
            return this.getElementValue(n.item(0));
     }

    /**
     * Getting first value from xml 
     * @param  xml String
     * @param tag String
     * */
    public String getFirstValueFromXml(String xml, String tag){
        Document doc = getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(tag);
        Node node = nl.item(0) ;
        String firstValue = getElementValue(node) ;
        return firstValue ;
    }
}

然后,在您的方法onResponse()

public void onResponse(String response) {
        XMLParser xmlParser = new XMLParser() ;
        String message = xmlParser.getFirstValueFromXml(xml,"Error") ;
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show();  
 } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM