ERLEDIGT
JA
JA
ANTWORTEN
2
2
ZUGRIFFE
2150
2150
EMPFEHLEN
-
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
-
23.03.05 16:47 #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
-
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
-
JDOM - Elemente nicht einlesbar wegen Namensräumen
Von Guntpat1981 im Forum JavaAntworten: 2Letzter Beitrag: 18.09.08, 11:36 -
JDOM - Einfügen mehrerer Elemente mit Attributen
Von Guntpat1981 im Forum Java GrundlagenAntworten: 1Letzter Beitrag: 26.08.08, 22:19 -
<li>-Elemente zählen?
Von herrgarnele im Forum Javascript & AjaxAntworten: 2Letzter Beitrag: 08.06.07, 14:44 -
JDOM - neue Elemente in bestehendes Dokument einfügen
Von lubu im Forum JavaAntworten: 2Letzter Beitrag: 06.06.06, 19:14 -
Elemente im Array zählen
Von schleckerbeck im Forum PHPAntworten: 4Letzter Beitrag: 29.11.04, 17:22





Zitieren

Login





