tutorials.de Buch-Aktion 05/2012
ERLEDIGT
JA
ANTWORTEN
2
ZUGRIFFE
2150
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    Tin80 Tin80 ist offline Grünschnabel
    Registriert seit
    Mar 2005
    Beiträge
    2
    Hallo zusammen

    Ich möchte gerne in einem XML-File zählen wieviele Elemente eines bestimmten Typ es gibt.
    Hier ein Auszug aus meinem XML, damit die Frage etwas klarer wird:

    Code :
    1
    2
    3
    4
    5
    
    <class name="Win32_NetworkAdapterConfiguration">
      <method name="EnableStatic" param="123.325.35.26"/>
      <method name="SetGatways" param="123.35.26.35"/>
      <restriction text="Error..."/>
    </class>

    Nun möchte ich zählen, wie oft das Element "method" in class vorkommt. Weiss jemand wie das geht?

    Vielen Dank für eure Hilfe.

    Gruss
    Tin
     

  2. #2
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo!

    Schau mal hier:
    Code :
    1
    2
    3
    4
    5
    6
    
    <?xml version="1.0"?>
    <class name="Win32_NetworkAdapterConfiguration">
        <method name="EnableStatic" param="123.325.35.26"/>
        <method name="SetGatways" param="123.35.26.35"/>
        <restriction text="Error..."/>
    </class>

    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
    
    /**
     * 
     */
    package de.tutorials;
     
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
     
    import org.xml.sax.Attributes;
    import org.xml.sax.helpers.DefaultHandler;
     
    /**
     * @author Administrator
     * 
     */
    public class XMLElementCounter {
     
        private int cnt;
     
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
            new XMLElementCounter().doIt();
        }
     
        private void doIt() throws Exception {
            SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
     
            parser.parse(XMLElementCounter.class.getResourceAsStream("foo.xml"),
                    new DefaultHandler() {
                        public void startElement(String uri, String localName,
                                String qName, Attributes attributes) {
                            if (qName.equals("method")) {
                                cnt++;
                            }
                        }
                    });
     
            System.out.println(cnt);
        }
    }

    ... oder Java 5 Mittel und XPath einsetzen:
    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
    
    /**
     * 
     */
    package de.tutorials;
     
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathFactory;
     
    import org.xml.sax.InputSource;
     
    /**
     * @author Administrator
     * 
     */
    public class XMLElementCounter {
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
            new XMLElementCounter().doIt();
        }
     
        private void doIt() throws Exception {
            System.out.println(XPathFactory.newInstance().newXPath().evaluate(
                    "count(//method)",
                    new InputSource(XMLElementCounter.class
                            .getResourceAsStream("foo.xml"))));
     
        }
    }

    Gruß Tom
     

  3. #3
    Tin80 Tin80 ist offline Grünschnabel
    Registriert seit
    Mar 2005
    Beiträge
    2
    Hallo

    Danke. Ich habe mittlerweile eine einfachere Lösung gefunden:

    Code :
    1
    2
    3
    4
    5
    
     
    Element root = doc.getRootElement();
    Element cimclass = root.getChild("cimclass");
    List methods = cimclass.getChildren("method"); 
    int countMethods = methods.size();

    Danke trotzdem.
    Gruss Tin
     

Ähnliche Themen

  1. Antworten: 2
    Letzter Beitrag: 18.09.08, 11:36
  2. JDOM - Einfügen mehrerer Elemente mit Attributen
    Von Guntpat1981 im Forum Java Grundlagen
    Antworten: 1
    Letzter Beitrag: 26.08.08, 22:19
  3. <li>-Elemente zählen?
    Von herrgarnele im Forum Javascript & Ajax
    Antworten: 2
    Letzter Beitrag: 08.06.07, 14:44
  4. Antworten: 2
    Letzter Beitrag: 06.06.06, 19:14
  5. Elemente im Array zählen
    Von schleckerbeck im Forum PHP
    Antworten: 4
    Letzter Beitrag: 29.11.04, 17:22