2017-05-11 7 views
-4

IDを介してXMLをJavaコードにリンクする方法は? たとえば、ボタンをクリックすると、レイヤーを変更したり、textViewからStringを与えたりします。AndroidでIDを介してJavaコードにリンクされたXML

+0

多くの詳細をこの質問に入れる必要があります。 XMLをJavaにリンクする簡単な方法の1つは、saxparserを使うことです。 – Whitecat

答えて

0

DOMパーサーまたはSAXパーサーの使用をお勧めします。 DOMのチュートリアルでは、XMLを読み込み、個々の要素を解析する方法について、hereが見つかりました。 SAXのチュートリアルはhereです。ここで

は、例えば、SAXパーサーです: パッケージca.ubc.cs.recommender.bugzilla.parser。

import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;

/** 
*  parse the XML information 
*  
* @author whitecat 
* 
*/ 
public class XMLParser extends DefaultHandler { 

    // Remember information being parsed 
    private StringBuffer accumulator; 
    private StringBuffer xmlFile; 

    public XMLParser(StringBuffer xmlFile) { 
     this.xmlFile = xmlFile; 
    } 

    /** 
    * Called at the start of the document (as the name suggests) 
    */ 
    public void startDocument() { 
     // Use accumulator to remember information parsed. Just initialize for 
     // now. 
     accumulator = new StringBuffer(); 
    } 

    /** 
    * Called when the parsing of an element starts. (e.g., <book>) 
    * 
    * Lookup documentation to learn meanings of parameters. 
    */ 
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) { 
     accumulator.setLength(0); 
    } 

    /** 
    * Called for values of elements 
    * 
    * Lookup documentation to learn meanings of parameters. 
    */ 
    public void characters(char[] temp, int start, int length) { 
     // Remember the value parsed 
     accumulator.append(temp, start, length); 
    } 

    /** 
    * Called when the end of an element is seen. (e.g., </title>) 
    * 
    * Lookup documentation to learn meanings of parameters. 
    * 
    * @throws SAXException 
    */ 
    public void endElement(String uri, String localName, String qName) throws SAXException { 
     if (qName.toLowerCase().equals("id") || qName.toLowerCase().equals("thetext")) { 
      xmlFile.append(accumulator.toString().trim().toLowerCase()); 
     } 

     // Reset the accumulator because we have seen the value 
     accumulator.setLength(0); 
    } 
} 

ここでは、XMLタグを訪問する際に文字列を収集するアキュムレータを設定します。それから終わりのタグに行くと私はIDを設定しました。私はあなたのXMLがどのようにセットアップされているのか分かりませんが、これはXMLを解析してJAVAで使用する方法の1つです。

関連する問題