XMLBeans/Beispiel: Unterschied zwischen den Versionen
Aus Joachim Schuster Wiki
(→Projekteinstellungen) |
|||
(2 dazwischenliegende Versionen von einem Benutzer werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
− | + | == Projekteinstellungen == | |
+ | Die erzeugte jar-Datei, die jar-Datei xmlbean.jar und jsr173_1.0_api.jar müssen den Libraries in den Projektproperties in Eclipse angegeben werden. | ||
− | + | === Fehlermeldungen === | |
− | + | 1. Ist die Datei xmlbean.jar nicht unter Libraries angegeben, so wird der Import '''import org.apache.xmlbeans.*;''' fehlschlagen und entsprechend gekennzeichnet. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | 2. Ist die Datei jsr173_1.0_api.jar nicht unter Libraries angegeben, so erhält man eine relativ wenig aussagende Fehlermeldung beim Ausführen des Programms: | |
− | + | <pre> | |
− | + | Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamException | |
+ | at java.lang.Class.getDeclaredMethods0(Native Method) | ||
+ | at java.lang.Class.privateGetDeclaredMethods(Unknown Source) | ||
+ | at java.lang.Class.getMethod0(Unknown Source) | ||
+ | at java.lang.Class.getMethod(Unknown Source) | ||
+ | at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:174) | ||
+ | at org.apache.xmlbeans.XmlBeans.buildNodeMethod(XmlBeans.java:195) | ||
+ | at org.apache.xmlbeans.XmlBeans.buildNodeToCursorMethod(XmlBeans.java:232) | ||
+ | at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:131) | ||
+ | at noNamespace.ItemsDocument$Factory.parse(ItemsDocument.java:126) | ||
+ | at firstxml.main(firstxml.java:19) | ||
+ | </pre> | ||
+ | |||
+ | == Beispielcode == | ||
+ | <pre> | ||
+ | import java.io.File; | ||
+ | import java.io.IOException; | ||
+ | import org.apache.xmlbeans.*; | ||
+ | import noNamespace.*; | ||
+ | import noNamespace.ItemDocument.Item; | ||
+ | |||
+ | public class firstxml { | ||
+ | public static void main(String[] args) { | ||
+ | File xmlFile = new File("./firstxmlbean.xml"); | ||
+ | ItemsDocument iDoc = null; | ||
+ | try { | ||
+ | iDoc = ItemsDocument.Factory.parse(xmlFile); | ||
+ | } catch (XmlException e) { | ||
+ | e.printStackTrace(); | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | Item[] items = (iDoc.getItems()).getItemArray(); | ||
+ | for (int i = 0; i < items.length; i++) | ||
+ | { | ||
+ | System.out.println(items[i]); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </pre> |
Aktuelle Version vom 29. Juli 2007, 14:40 Uhr
Projekteinstellungen
Die erzeugte jar-Datei, die jar-Datei xmlbean.jar und jsr173_1.0_api.jar müssen den Libraries in den Projektproperties in Eclipse angegeben werden.
Fehlermeldungen
1. Ist die Datei xmlbean.jar nicht unter Libraries angegeben, so wird der Import import org.apache.xmlbeans.*; fehlschlagen und entsprechend gekennzeichnet.
2. Ist die Datei jsr173_1.0_api.jar nicht unter Libraries angegeben, so erhält man eine relativ wenig aussagende Fehlermeldung beim Ausführen des Programms:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/stream/XMLStreamException at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) at java.lang.Class.getMethod0(Unknown Source) at java.lang.Class.getMethod(Unknown Source) at org.apache.xmlbeans.XmlBeans.buildMethod(XmlBeans.java:174) at org.apache.xmlbeans.XmlBeans.buildNodeMethod(XmlBeans.java:195) at org.apache.xmlbeans.XmlBeans.buildNodeToCursorMethod(XmlBeans.java:232) at org.apache.xmlbeans.XmlBeans.<clinit>(XmlBeans.java:131) at noNamespace.ItemsDocument$Factory.parse(ItemsDocument.java:126) at firstxml.main(firstxml.java:19)
Beispielcode
import java.io.File; import java.io.IOException; import org.apache.xmlbeans.*; import noNamespace.*; import noNamespace.ItemDocument.Item; public class firstxml { public static void main(String[] args) { File xmlFile = new File("./firstxmlbean.xml"); ItemsDocument iDoc = null; try { iDoc = ItemsDocument.Factory.parse(xmlFile); } catch (XmlException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Item[] items = (iDoc.getItems()).getItemArray(); for (int i = 0; i < items.length; i++) { System.out.println(items[i]); } } }