Android - 如何处理 XML 中的 Web 服务?

标签 android xml web-services

我有一个示例 XML 文件如下:

 <?xml version="1.0"?>
  <h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <h:head>
<h:title>tutorial</h:title>
<model>
  <instance>
    <tutorial id="tutorial">
      <name/>
      <age/>
      <gender/>
      <photo/>
      <date/>
      <location/>
      <thanks/>
      <start/>
      <end/>
      <today/>
      <deviceid/>
      <subscriberid/>
      <simserial/>
      <phonenumber/>
      <meta>
        <instanceID/>
      </meta>
    </tutorial>
  </instance>
  <bind nodeset="/tutorial/name" required="true()" type="string"/>
  <bind constraint=". &gt; 0 and . &lt; 120" jr:constraintMsg="That's not a valid age!" nodeset="/tutorial/age" required="true()" type="int"/>
  <bind nodeset="/tutorial/gender" type="select1"/>
  <bind nodeset="/tutorial/photo" type="binary"/>
  <bind nodeset="/tutorial/date" type="date"/>
  <bind nodeset="/tutorial/location" type="geopoint"/>
  <bind nodeset="/tutorial/thanks" readonly="true()" type="string"/>
  <bind jr:preload="timestamp" jr:preloadParams="start" nodeset="/tutorial/start" type="dateTime"/>
  <bind jr:preload="timestamp" jr:preloadParams="end" nodeset="/tutorial/end" type="dateTime"/>
  <bind jr:preload="date" jr:preloadParams="today" nodeset="/tutorial/today" type="date"/>
  <bind jr:preload="property" jr:preloadParams="deviceid" nodeset="/tutorial/deviceid" type="string"/>
  <bind jr:preload="property" jr:preloadParams="subscriberid" nodeset="/tutorial/subscriberid" type="string"/>
  <bind jr:preload="property" jr:preloadParams="deviceid" nodeset="/tutorial/simserial" type="string"/>
  <bind jr:preload="property" jr:preloadParams="phonenumber" nodeset="/tutorial/phonenumber" type="string"/>
  <bind calculate="concat('uuid:', uuid())" nodeset="/tutorial/meta/instanceID" readonly="true()" type="string"/>
</model>
</h:head>
<h:body>
<input ref="/tutorial/name">
  <label>What's your name?</label>
</input>
<input ref="/tutorial/age">
  <label>How old are you?</label>
</input>
<select1 ref="/tutorial/gender">
  <label>Gender</label>
  <item>
    <label>Male</label>
    <value>male</value>
  </item>
  <item>
    <label>Female</label>
    <value>female</value>
  </item>
</select1>
<upload mediatype="image/*" ref="/tutorial/photo">
  <label>Please Take a picture</label>
</upload>
<input ref="/tutorial/date">
  <label>Date</label>
</input>
<input ref="/tutorial/location">
  <label>Where are you?</label>
  <hint>You need to be outside for your GPS to work.</hint>
</input>
<input ref="/tutorial/thanks">
  <label>Thanks for your time <output value="/tutorial/name"/>!</label>
</input>
</h:body>
</h:html>

现在我要填写<input ref="/tutorial/name">数据作为网络服务的响应?我有一个正确给出响应的网络服务。请帮助我....

最佳答案

替换 <input ref="/tutorial/name">标签。您必须遵循这三个步骤。

1) 查找<input ref="/tutorial/name"> XML 文件中的 TAG。

2) 执行更新、删除、替换您想要的任何功能。

3)将内容写入XML文件

第 1 步:查找节点:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document dom = db.parse(FILE);
Element root = dom.getDocumentElement();

//get node-list,
NodeList inputNodeList = root.getElementsByTagName("input");

//iterate over all input node to find the desired one.
for(int i=0;i<inputNodeList.length();i++){
  Node inputNode = inputNodeList.item(i); //get a single node.
  String refStr = inputNode.getAttributes().getNamedItem("ref").getNodeValue();
  if(refStr.compareTo("/tutorial/name")==0){

     // Bingo, You have find the NODE. Now you can do any operations like delete, edit whatever you want.
   }
}

第 2 步:执行替换操作。

您可以使用删除节点。

inputNode.getParentNode().removeChild(inputNode);

如果要替换节点,请查看replaceChild()方法。

基本思路是,

  • 根据响应字符串创建节点。引用this link用于从字符串创建 xml 节点。
  • 使用 inputNode.getParentNode() 查找 inputNode 的父节点
  • 使用 replaceChild() 方法用新节点替换旧节点

关于 replaceChild() 方法的例子你可以引用 this link . 这是一个使用 DOM 解析器修改 XML 文件的好教程 - TUTORIAL .

第三步:将内容写入XML文件。

TransformerFactory transformerFactory = TransformerFactory.newInstance();  
Transformer transformer = transformerFactory.newTransformer();   
DOMSource source = new DOMSource(doc);   
StreamResult result = new StreamResult(selectedFile); 
transformer.transform(source, result); 

希望这会给您一些提示,帮助您完成所需的任务。

关于Android - 如何处理 XML 中的 Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14152603/

相关文章:

java - 将 XML 放入嵌套布局后,数学逻辑停止工作

c# - 为什么 XAML(WPF) 不在我的 XML 命名空间定义中搜索控件?

web-services - 如何为 Web 服务和 Android 应用程序使用 HTTPS?

javascript - 正则表达式:Javascript、XML、字符串

基于 C++ rest 的 Web 服务客户端实现

java - Tomcat和防洪

android - 使用android处理不同编码的文件

android自动测试方向改变

android.support.v4.app.NotificationCompat 已弃用

xml - 如何从 json 文件(或 xml 文件)创建 vb.net 对象类