9

以下のようなサンプル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">Webサービスからの応答として来るデータを入力したいですか?正しく応答するWebサービスがあります。私を助けてください....

4

2 に答える 2

5

<input ref="/tutorial/name">タグを置き換えるには。この 3 つの手順に従う必要があります。

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()メソッドを参照してください。

基本的な考え方は、

  • 応答文字列からノードを作成します。文字列から xml ノードを作成するには、このリンクを参照してください。
  • inputNode.getParentNode() を使用して、inputNode の親を見つけます
  • 古いノードを新しいノードに置き換えるには、replaceChild() メソッドを使用します

replaceChild() メソッドの例については、このリンクを参照してください。これは、DOM パーサーを使用して XML ファイルを変更するための優れたチュートリアルです - TUTORIAL

ステップ 3 : コンテンツを XML ファイルに書き込みます。

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

これにより、目的のタスクを達成するためのヒントが得られることを願っています。

于 2013-01-23T04:53:29.800 に答える
4

アプリで xml 解析を処理するために SAXParser を使用しています。

try {
        /**
         * Create a new instance of the SAX parser
         **/
        SAXParserFactory saxPF = SAXParserFactory.newInstance();
        SAXParser saxP = saxPF.newSAXParser();
        XMLReader xmlR = saxP.getXMLReader();
        URL url = new URL("http:Your URL"); // URL of the XML
        XMLHandler myXMLHandler = new XMLHandler();
        xmlR.setContentHandler(myXMLHandler);
        xmlR.parse(new InputSource(url.openStream()));

    } catch (Exception e) {
        e.printStackTrace();
    }

また、これに関する優れたチュートリアルもあります: http://mobile.tutsplus.com/tutorials/android/android-sdk-build-a-simple-sax-parser/

于 2013-01-22T20:19:42.813 に答える