在单个请求上处理两个不同的 xml 文件:Android

发布于 2024-11-30 08:22:34 字数 856 浏览 2 评论 0原文

首先,我没有找到更好的标题来回答我的问题,所以如果您有一些话,请更新。

我正在做什么:

目前,我为此目的发送两个请求(一个用于用户验证,另一个用于数据,如果用户获得授权)。但我想使用单个请求。

我想要什么:

我将以 POST 方法向服务器发送请求,服务器将根据我的请求发送 XML 作为响应。

例如:

  • 我正在将用户和密码发送到服务器。
  • 服务器验证该用户。
  • 如果用户授权,服务器将发送响应 xml 作为

    在此处输入图像描述

  • 如果身份验证失败,服务器将响应发送为

在此处输入图像描述

我遇到的问题:

我无法处理 XML 数据,实际上无法识别 xml 数据。

我正在使用 android.sax 解析器,并且为两种类型的 xml 响应创建了两个解析器类,但是如何根据响应确定应该使用哪个解析器类?

更新:

这是我的实际 xml

在此处输入图像描述在此处输入图像描述

那么我如何使用单个解析器来解析它?

First off all I didn't find better title for my question, so please update if you have some words.

What I am doing :

In current I am sending two request for this purpose(One for user validation, another for data, if user is authorised). But I want to use single request.

What I want :

I will send a request as POST method to server where server will send a XML as response depends on my request.

For example :

  • I am sending user and password to server.
  • Server authenticate that user.
  • If user authorised, server sends response xml as

    enter image description here

  • If authentication failed, server sends response as

enter image description here

Problem I faced :

I am not able to handle XML data, actually not identify the xml data.

I am using android.sax parser and I created two parser classes for both type of xml response, But how can I identify which parser class should I use depends on response?

Update :

Here is my actual xml

enter image description hereenter image description here

So how can I parse it, using single parser?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风也等你 2024-12-07 08:22:34

我会使用一种数据结构,允许解析器查看它是否失败。
示例:

public class XMLResponse {
 private boolean hasFailed;
 private Employee employee;
 public void setFailure(boolean in) {
    this.hasFailed=in;
 }
 public void setEmployee(Employee in) {
    this.employee=in;
 }
}

在您的解析器中,查看响应是否为假。这是基于以下事实:如果成功,来自 Web 服务的响应不包含响应标记。

这是一个解析器的示例,在使用之前可能需要一些调整。仅当您的回复中只有 1 名员工时,它才有用,否则您需要使用列表。

public class XMLHandler extends DefaultHandler {
private XMLResponse myResponse;
private Employee employee;
public XMLResponse getParsedData() { 
    return this.myResponse;
}
@Override
public void startDocument() throws SAXException {
    myResponse = new XMLResponse();
    employee = new Employee();
}

@Override
public void startElement(String namespaceURI, String localName,

        String qName, Attributes atts) throws SAXException {
    buffer = new StringBuffer();
    if(localName.equals("employee")) {
        employee.setId(atts.getValue("id"));
    }
}
@Override

public void endElement(String namespaceURI, String localName, String qName)

throws SAXException {

    if(localName.equals("response")) {
        if(buffer.toString().contains("failure")) {
            myResponse.setFailure(true);
        }
    }
    else if(localName.equals("info")) {
        /*
         * This is only an example, could bee employee or whatever. You should use the startElement to get the tag. 
         */
    }
    else if(localName.equals("name")) {
        employee.setName(buffer.toString());
    }
    else if(localName.equals("age")) {
        employee.setAge(buffer.toString());
    }
    else if(localName.equals("employee")) {
        myResponse.setEmployee(employee);
    }


}

/** Gets be called on the following structure:

 * <tag>characters</tag> */


StringBuffer buffer;
@Override
public void characters(char ch[], int start, int length) {

    buffer.append(ch,start,length);
}

I would use a data structure which allows for the parser to see if it has failed or not.
Example:

public class XMLResponse {
 private boolean hasFailed;
 private Employee employee;
 public void setFailure(boolean in) {
    this.hasFailed=in;
 }
 public void setEmployee(Employee in) {
    this.employee=in;
 }
}

And in your parser, see if the response is false or not. This is based on the fact that your response from the webservice does not contain the response tag if its successful.

Here is an example of a parser, might need some tweaks before it can be used. It is only useful if you only get 1 employee in your response, otherwise you need to use a list.

public class XMLHandler extends DefaultHandler {
private XMLResponse myResponse;
private Employee employee;
public XMLResponse getParsedData() { 
    return this.myResponse;
}
@Override
public void startDocument() throws SAXException {
    myResponse = new XMLResponse();
    employee = new Employee();
}

@Override
public void startElement(String namespaceURI, String localName,

        String qName, Attributes atts) throws SAXException {
    buffer = new StringBuffer();
    if(localName.equals("employee")) {
        employee.setId(atts.getValue("id"));
    }
}
@Override

public void endElement(String namespaceURI, String localName, String qName)

throws SAXException {

    if(localName.equals("response")) {
        if(buffer.toString().contains("failure")) {
            myResponse.setFailure(true);
        }
    }
    else if(localName.equals("info")) {
        /*
         * This is only an example, could bee employee or whatever. You should use the startElement to get the tag. 
         */
    }
    else if(localName.equals("name")) {
        employee.setName(buffer.toString());
    }
    else if(localName.equals("age")) {
        employee.setAge(buffer.toString());
    }
    else if(localName.equals("employee")) {
        myResponse.setEmployee(employee);
    }


}

/** Gets be called on the following structure:

 * <tag>characters</tag> */


StringBuffer buffer;
@Override
public void characters(char ch[], int start, int length) {

    buffer.append(ch,start,length);
}
三月梨花 2024-12-07 08:22:34

答案是拥有一个专门适合传递响应值的响应类。常见的结构是:

  • IsError
  • Message
  • Data

这个想法是,对于每个响应,您可以检查它是否是错误......如果是,您可以将 Message 中的值显示给用户。如果不是,则获取“数据”并将其反序列化

The answer is to have a response class specifically suited for communicating response values. A common structure is:

  • IsError
  • Message
  • Data

The idea is that for every response, you can check to see whether it was an error ... if so, you can display the value in Message to the user. IF not, take the 'data', and deserialize it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文