Brauche Hilfe mit XMLBeans

Loki2

Erfahrenes Mitglied
Hallo,

ich hoffe hier kennt sich jemand ein bisschen mit den XMLBeans aus oder kann mir vielleicht auch so weiterhelfen.
Mit den XMLBeans kann ich mir ja aus einem XML-Schema File die entsprechenden Klassen für die Daten in der XML Datei generieren lassen und über diese Klassen dann darauf zu greifen. So weit funktioniert das auch. Mein Problem ist jetzt das ich nicht weiss wie ich an die Namen der einzelnen Elementeigenschaften ran komme bzw. abfragen kann wieviele Eigenschaften ein so ein Element hat um dann alle abzufragen und darzustellen. Nehmen wir mal folgenden XML Teil:
Code:
   <Header version="01.01" date="19802611" classification="Unknown" validationStatus="Working">
  	<AuthorSet>
  	  <Author>blablalbla</Author>
  	  <Author>blablalbla</Author>
  	  <Author>blablalbla</Author>
  	</AuthorSet>
    </Header>

Mit
Code:
.getHeader().getVersion()
bekomme ich die version, mit
Code:
.getHeader().getDate()
das Datum usw. Mir ist aber noch völlig unklar wie ich abfragen kann welche Eigenschaften das Header-Element hat um dann alle Namen und Werte der Eigenschaften mit einer for-Schleife abzufragen und ausgeben zu lassen.
Ich hoffe hier kennt sich jemand mit den XMLBeans aus und kann mir weiterhelfen.

Gruß und so
Loki2
 
Hallo!

Habe das XMLBeans Beispiel MixedContent aus der 2.0er Distribution ein wenig modifiziert (zwei Attribute hinzugefügt) und danach die Klassen für das XML Binding neu generieren lassen.

XMLSchema:
Code:
  <?xml version="1.0" encoding="UTF-8"?>
  <!--
  Copyright 2004 The Apache Software Foundation
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
  http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
   -->
  <xs:schema targetNamespace="http://xmlbeans.apache.org/samples/cursor/mixedcontent"
  	xmlns:mc="http://xmlbeans.apache.org/samples/cursor/mixedcontent"
  	xmlns:xs="http://www.w3.org/2001/XMLSchema"
  	elementFormDefault="qualified">
  	<xs:complexType name="descriptionType" mixed="true">
  		<xs:choice minOccurs="0" maxOccurs="unbounded">
  			<xs:element name="link" type="mc:linkType"/>
  		</xs:choice>
  	</xs:complexType>
  	<xs:element name="inventory">
  		<xs:complexType>
  			<xs:sequence>
 		 	<xs:element name="item" type="mc:itemType" maxOccurs="unbounded"/>
  			</xs:sequence>
  		</xs:complexType>
  	</xs:element>
  	<xs:complexType name="itemType">
  		<xs:sequence>
  			<xs:element name="name" type="xs:string"/>
 			<xs:element name="description" type="mc:descriptionType"/>
  			<xs:element name="price" type="xs:double"/>
  			<xs:element name="quantity" type="xs:int"/>
  		</xs:sequence>
  		<xs:attribute name="id" type="xs:int" use="required"/>
  		<xs:attribute name="foo" type="xs:string" use="required"/>
  		<xs:attribute name="bar" type="xs:string" use="required"/>
  	</xs:complexType>
  	<xs:complexType name="linkType">
  		<xs:simpleContent>
  			<xs:extension base="xs:string">
 		 	<xs:attribute name="id" type="xs:int" use="required"/>
  			</xs:extension>
  		</xs:simpleContent>
  	</xs:complexType>
  </xs:schema>

Das neue XML-Dokument:
Code:
  <!--
  Copyright 2004 The Apache Software Foundation
  
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
  http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
   -->
  <inventory xmlns="http://xmlbeans.apache.org/samples/cursor/mixedcontent">
  	<item id="897946" foo="hallo1" bar="welt1">
  		<name>locking flange harbinger</name>
  		<description>Completely myopic gyrating mill-flange.</description>
  		<price>21.79</price>
  		<quantity>4594</quantity>
  	</item>
  	<item id="745621" foo="hallo2" bar="welt2">
  		<name>protaic calliphange</name>
  	    <description>Asymmetrical flared-gill spongimass. Complements the locking flange harbinger
  			with perfect precision.</description>
  		<price>19.95</price>
  		<quantity>2</quantity>
  	</item>
  	<item id="784269" foo="hallo3" bar="welt3">
  		<name>gyrating mill-flange</name>
 	 <description>Polymorphic atrophical mylobium. Not compatible with any other device ever made.</description>
  		<price>.99</price>
  		<quantity>1205987</quantity>
  	</item>
  </inventory>

Schau mal hier:
Code:
  package de.tutorials;
  
  import java.io.File;
  
  import org.apache.xmlbeans.samples.cursor.mixedcontent.InventoryDocument;
  import org.apache.xmlbeans.samples.cursor.mixedcontent.ItemType;
  import org.apache.xmlbeans.samples.cursor.mixedcontent.InventoryDocument.Inventory;
  import org.w3c.dom.NamedNodeMap;
  import org.w3c.dom.Node;
  
  public class XMLBeansExample {
  
  	/**
  	 * @param args
  	 */
  	public static void main(String[] args) throws Exception {
  		InventoryDocument inventoryDocument = InventoryDocument.Factory
  				.parse(new File(
 		 		 "E:/stuff/xmlbeans/2.0.0/xmlbeans-2.0.0/samples/MixedContent/xml/inventoryitems.xml"));
  		
  		Inventory inventory = inventoryDocument.getInventory();
  		ItemType[] items = inventory.getItemArray();
  		for (int i = 0; i < items.length; i++) {
  			ItemType item = items[i];
  			System.out.println(item.getId()+": ");
 			NamedNodeMap attributes = item.getDomNode().getAttributes();
 			for(int len = attributes.getLength(), j = 0; j < len;j++){
 				Node attributeNode = attributes.item(j);
 		 	System.out.print(attributeNode.getNodeName() + ": " + attributeNode.getNodeValue() + " ");
  			}
  			System.out.println();
  		}
  		
  	}
  
  }

Ausgabe:
Code:
  897946: 
  id: 897946 foo: hallo1 bar: welt1 
  745621: 
  id: 745621 foo: hallo2 bar: welt2 
  784269: 
  id: 784269 foo: hallo3 bar: welt3

Gruß Tom
 
Zurück