2011-10-01 42 views
-1

如何在屏幕上实现下xml文件的代码如何实现获取xml数据并保存inot xml文件的代码?

<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 available</Question> 
     <Ans></Ans> 
    </key> 
    </keys> 
</root> 

显示:

列表形式 是车黑的颜色吗?复选框 行李收集器是否可用?复选框

如果选中该复选框 - 否则值为否。

保存按钮:我们需要将它保存到一个xml文件中。所以任何人都可以帮助我解决问题。

+1

好 - 首先你已经接受Stack Overflow的25个答案中的1个(但是我会给你带来疑问的好处,并假设24个问题收到了不适合你的答案)。其次 - 什么?!##!你在说什么?请重新提出您的问题,以便人类能够理解它:>。你还没有发布任何XML,你发布了一堆关于汽车,颜色和报纸的随机问题! – Jack

+0

是汽车的颜色 黑? 是汽车的颜色 黑? 是新闻用纸 华尔街? 清香气味 Lavendor? 是可用的行李收集 这是我发现的,而editing.Before张贴问题,你必须确保它是否在正确的格式或not.What究竟做你要?你想要形成和XML或解析这个XML? –

回答

1

通过您的问题,它显示您想要获取XML的内容。为此你需要解析XML。在其他类调用创建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; 

} 

那么这个功能的getXML

声明的int和INT y随一些价值

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"); 

     } 

}

相关问题