2013-01-04 19 views
9

Mam przykładowy plik XML jak poniżej:Android - jak obsługiwać Webservice w 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> 

Teraz chcę wypełnić <input ref="/tutorial/name"> z danych pochodzących jako odpowiedź z serwisu WWW? Mam usługę internetową, która udziela odpowiedzi poprawnie. Proszę mi pomóc ....

+11

Pytanie wydaje się być podstawową analizą, procesem i odpowiedzią. Będziesz musiał podać więcej szczegółów na temat swojego problemu, zanim ktokolwiek może pomóc. –

+0

Jaki jest format otrzymywanej odpowiedzi?
Jeśli jest tylko jedna wartość do wyboru z odpowiedzi i miejsce w xml, to dobrze jest przejść z funkcjami łańcuchowymi przez podniesienie indeksu znacznika i uzyskanie podciągu jako wartości i wykonanie tego samego w pliku xml i zastąpienie. Jeśli jest więcej niż jedna wartość, niż można przejść z parserami xml, używając parsera DOM, takiego jak dom4j http://dom4j.sourceforge.net/. Jeśli odpowiedź znajduje się w jsonie, użyj biblioteki Google Gson http://code.google.com/p/google-gson/. – Omar

Odpowiedz

5

Wymiana <input ref="/tutorial/name"> tag. Musisz wykonać te trzy kroki.

1) Znajdź <input ref="/tutorial/name"> TAG w pliku XML.

2) Aktualizuj, usuń, zastąp dowolną żądaną funkcję.

3) Wpisz zawartość do pliku XML

Krok 1: Znajdź Node:

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. 
    } 
} 

Krok 2: Czy Wymień operację.

można usunąć węzeł za pomocą.

inputNode.getParentNode().removeChild(inputNode); 

Jeśli chcesz wymienić węzeł, spójrz na metodę replaceChild().

Podstawowa idea jest

  • Tworzenie węzła z łańcucha reakcji. Zajrzyj do this link, aby utworzyć węzeł XML z łańcucha.
  • Znajdź obiekt macierzysty węzła wejściowego przy użyciu metody inputNode.getParentNode().
  • Użyj metody replaceChild(), aby zastąpić stary węzeł nowym węzłem:.

Dla przykładu metody replaceChild() można polecić this link. Oto ładny samouczek do modyfikowania pliku XML przy użyciu parsera DOM - TUTORIAL.

Krok 3: Zapisz treść w pliku XML.

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

Hope, to daje pewną wskazówkę na temat osiągnięcia żądanego zadania.

4

Używam SAXParser do obsługi parsowania xml w mojej aplikacji.

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(); 
    } 

Również tutaj jest dobry tutorial na ten temat: http://mobile.tutsplus.com/tutorials/android/android-sdk-build-a-simple-sax-parser/

Powiązane problemy