tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
0
ZUGRIFFE
1006
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    theCatalist theCatalist ist offline Grünschnabel
    Registriert seit
    Aug 2003
    Ort
    von den Bergen
    Beiträge
    1
    Hallo zusammen,
    ich habe folgende problem stellung: ich habe ein bestehendes xml, von diesem XML File möchte ich die Tags<name> <affichage> <IW> und <wagolink> auslesen und auf dem handy (mobiltelefon, soory bin aus dem nachbarland ) darstellen, mein Problem ist jetzt wie parse ich das File so das es beim Tag <affichcge>button</affichage> das Programm merkt hier muss ein Button dar gestellt werden. Weiter mus dan durch drücken des Buttons der tag <wagolink> ausgelöst werden um den Befehl an die Steuerung zu übergeben.
    oder bin ich so auf dem Holz weg? Danke für eure Hilfe******
    XML File:
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    <?xml version="1.0"?>
     
       <items>
            <item id="0">
                <title>Temperatur</title>
                <name>Aussentemperatur</name>
                <description>Temperatur1</description>
                <type>lineaire</type>
                <unit>C</unit>
                <coef>1</coef>
                <offset>0</offset>
                <IW><!--#READPI ADR=MW210&FORMAT=%d--></IW>
            </item>
            <item id="1">
                <title>Datum</title>
                <name>Datum</name>
                <description>Aktuelles Datum</description>
                <type>direct</type>
                <affichage>date</affichage>
                <IW><!--#GETTIMEDATE FORMAT=%d.%m.%y %H:%M:%S--></IW>
            </item>
            <item id="2">
                <title>Praesentation</title>
                <name>Praesentation</name>
                <description>Praesentation</description>
                <type>direct</type>
                <affichage>button</affichage>
                <IW><!--#READPI ADR=MW214&FORMAT=%d--></IW>
                <wagolink>http://192.168.92.100/WRITEPI?ADR1=MX200.1amp;VALUE1=1&amp;FORMAT1=%d</wagolink>
            </item>

    xmlhandler.java

    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;
     
    public class MyXMLHandler extends DefaultHandler {
        Boolean currentElement = false;
        String currentValue = null;
      public static SitesList sitesList = null;
     
     
        public static SitesList getSitesList() {
            return sitesList;
        }
     
        public static void setSitesList(SitesList sitesList) {
            MyXMLHandler.sitesList = sitesList;
        }
     
        /** Called when tag starts ( ex:- <name>AndroidPeople</name> 
         * -- <name> )*/
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            currentElement = true;
            
            if (localName.equals("items"))
            {
                /** Start */
                sitesList = new SitesList();
            } else if (localName.equals("item")) {
                /** Get attribute value */
                String attr = attributes.getValue("id");
                sitesList.setid(attr);
            }
            else if (localName.equals("affichage")) 
                sitesList.setaffichage(currentValue);
         }
             
         
        
     
        /** Called when tag closing ( ex:- <name>AndroidPeople</name> 
         * -- </name> )*/
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            
            
            
            currentElement = false;
            /** set value */
            if (localName.equalsIgnoreCase("title"))
                sitesList.settitle(currentValue);
            if (localName.equalsIgnoreCase("affichage"))
                
                sitesList.setaffichage(currentValue);
            else if (localName.equalsIgnoreCase("item"))
                sitesList.setitem(currentValue);
     
        }
     
     
     
     
        /** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
         * -- to get AndroidPeople Character ) */
        @Override
        public void characters(char[] ch, int start, int length)
        throws SAXException {
         if (currentElement) {
                       currentValue = new String(ch, start, length);
                        currentElement = false;
                    }
                 
                    }
                 
        }

    xmlparser.java
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    
    public class XMLParsingExample extends Activity {
     
        /** Create Object For SiteList Class */
        SitesList sitesList = null;
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
     
            /** Create a new layout to display the view */
             ScrollView sView =  new ScrollView (this);
             LinearLayout layout = new LinearLayout(this);
             layout.setOrientation(LinearLayout.VERTICAL);
             sView.addView(layout);
     
            /** Create a new textview array to display the results */
            TextView title[];
            TextView item[];
            TextView id[];
            Button affichage[];
            
            try {
                
                /** Handling XML */
                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();
     
                /** Send URL to parse XML Tags */
                URL sourceUrl = new URL(
                        "http://172.17.1.104/LEHMANN.xml");
     
                /** Create handler to handle XML Tags ( extends DefaultHandler ) */
                MyXMLHandler myXMLHandler = new MyXMLHandler();
                xr.setContentHandler(myXMLHandler);
                xr.parse(new InputSource(sourceUrl.openStream()));
                
            } catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
            }
     
            /** Get result from MyXMLHandler SitlesList Object */
            sitesList = MyXMLHandler.sitesList;
     
            /** Assign textview array lenght by arraylist size */
            title = new TextView[sitesList.gettitle().size()];
            item = new TextView[sitesList.gettitle().size()];
            id = new TextView[sitesList.gettitle().size()];
            affichage = new Button[sitesList.gettitle().size()];
            
            /** Set the result text in textview and add it to layout */
            for (int i = 0; i < sitesList.gettitle().size(); i++) {
                title[i] = new TextView(this);
                title[i].setText("Name = "+sitesList.gettitle().get(i));
                item[i] = new TextView(this);
                item[i].setText("Website = "+sitesList.getitem().get(i));
                id[i] = new TextView(this);
                id[i].setText("Website Category = "+sitesList.getid().get(i));
                affichage[i] = new Button(this);
                affichage[i].setText("Licht AN/AUS "+sitesList.getaffichage().get(i));
     
                layout.addView(title[i]);
                layout.addView(item[i]);
                layout.addView(id[i]);
                layout.addView(affichage[i]);
            }
     
            /** Set the layout view to display */
            this.setContentView(sView);
     
        }
    }

    siteslist.java
    Code :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    
    import java.util.ArrayList;
     
    /** Contains getter and setter method for varialbles  */
    public class SitesList {
     
        /** Variables */
        private ArrayList<String> title = new ArrayList<String>();
        private ArrayList<String> affichage = new ArrayList<String>();
        private ArrayList<String> item = new ArrayList<String>();
        private ArrayList<String> id = new ArrayList<String>();
        private ArrayList<String> IW = new ArrayList<String>();
     
        
        /** In Setter method default it will return arraylist 
         *  change that to add  */
        
        public ArrayList<String> gettitle() {
            return title;
        }
     
        public void settitle(String title) {
            this.title.add(title);
        }
        
        public ArrayList<String> getaffichage() {
            return affichage;
        }
     
        public void setaffichage(String affichage) {
            this.affichage.add(affichage);
        }
        public ArrayList<String> getitem() {
            return item;
        }
     
        public void setitem(String item) {
            this.item.add(item);
        }
        
        public ArrayList<String> getid() {
            return id;
        }
     
        public void setid(String id) {
            this.id.add(id);
        }
        
        public ArrayList<String> getIW() {
            return IW;
        }
     
        public void setIW(String IW) {
            this.IW.add(IW);
        }
     
     
     
    }
    Geändert von theCatalist (25.08.10 um 19:32 Uhr)
     

Ähnliche Themen

  1. X und Y Achse eines Charts manipulieren/anpassen
    Von filigrani im Forum Adobe Flex & AIR
    Antworten: 1
    Letzter Beitrag: 23.01.09, 15:09
  2. Wie Header eines Streams manipulieren?
    Von Thopeto im Forum Enterprise Java (JEE, J2EE, Spring & Co.)
    Antworten: 11
    Letzter Beitrag: 24.10.08, 12:36
  3. Alter eines Files
    Von Horrortubby im Forum Java
    Antworten: 3
    Letzter Beitrag: 19.07.07, 15:49
  4. Rechte eines Files
    Von Pre7ender im Forum PHP
    Antworten: 4
    Letzter Beitrag: 26.09.06, 10:12
  5. Bitrate eines .avi Files
    Von cheating_phil im Forum Videoschnitt, Videotechnik & -produktion
    Antworten: 0
    Letzter Beitrag: 23.06.03, 07:58

Stichworte