开发者

How to implement code for get the xml data and save inot xml file?

开发者 https://www.devze.com 2023-04-09 13:17 出处:网络
How to implement code for below xml file <root> <keys> <key> <Question>Is the color of the car</Question>

How to implement code for below xml file

<root>
  <keys>
    <key>
      <Question>Is the color of the car</Question>
      <Ans>black?</Ans>
    </key>
    <key>
      <Question>Is the color of the car</Question>
      <Ans>black?</Ans>
    </key>
    <key>
      <Question>Is the news paper </Question>
      <Ans>wallstreet?</Ans>
    </key>
    <key>
      <Question>fragrance odor</Question>
      <Ans>Lavendor?</Ans>
    </key>
    <key>
      <Question>Is the baggage collector availa开发者_StackOverflow中文版ble</Question>
      <Ans></Ans>
    </key>
  </keys>
</root>

Display on the screen as:

List form Is the color of the car black? Check box Is the baggage collector available? Check box

If the check box is checked - Yes otherwise value is no.

On save button: we need to save it into a xml file. So any one help me to solve the problem.


By your question it appears you want to fetch contents of XML. For that you need to parse the XML. Create an XMLClass.java

public class XMLfunctions {

public final static Document XMLfromString(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) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

/** Returns element value
  * @param elem element (it is XML tag)
  * @return Element value otherwise empty String
  */
 public final static String getElementValue( Node elem ) {
     Node kid;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                 if( kid.getNodeType() == Node.TEXT_NODE  ){
                     return kid.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 public static String getXML(int s, int y){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            String link = "//YOUR URL HERE" add LIMITS IF ANY
                 for ex: String link = "www.ab.com/index.php?action=gq&start="+s+"&limit="+y;
// it will give you values from 1 to 10 or 1 to 50 whatever you mention         
            HttpPost httpPost = new HttpPost(link);

            System.out.println("Httppost="+httpPost);

            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();

            System.out.println("HttpEntity="+httpEntity);

            line = EntityUtils.toString(httpEntity);

            System.out.println("Line="+line);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }

        return line;

}

Then in other class call this getXML function

declare int s and int y with some value

   String xmls = XMLfunctions2.getXML(loginid);    

    Document docs = XMLfunctions2.XMLfromString(xmls);
    NodeList node = docs.getElementsByTagName("start");

for (int i = 0; i < node.getLength(); i++) {    

        org.w3c.dom.Element e = (org.w3c.dom.Element)node.item(i);
        if(i==0)

        {
            quest= XMLfunctions.getValue(e, "Question");
            ans=XMLfunctions.getValue(e, "Answer");
            key=XMLfunctions.getValue(e, "key");

        }

}

0

精彩评论

暂无评论...
验证码 换一张
取 消