File objekt in xml speichern?

  • Themenstarter Themenstarter JaireMichel
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
J

JaireMichel

Gast
Hallo,

ist es möglich File objekte anstatt die VErzeichnis-Strings in xml files zu speichern und diese auszulesen als File objekte?
 
Ja, die werden als Base64 gespeichert. Dafür empfielt sich ein xml-Binding Framework wie z.B. jaxb oder XMLBeans. Du kannst die Kodierungen auch selber machen. Ich glaub im commons-codec befindet sich ein Base64En- und Decoder.
 
Niki hat gesagt.:
Ja, die werden als Base64 gespeichert. Dafür empfielt sich ein xml-Binding Framework wie z.B. jaxb oder XMLBeans. Du kannst die Kodierungen auch selber machen. Ich glaub im commons-codec befindet sich ein Base64En- und Decoder.

hoi du,

ich benutze eh schone JAXB 2.x doch lese ich in meinem Buch nichts von File in xml speichern Möglichkeiten?? Wenn ich google benutze und suche nach: xml file kommt natürlich nur wie man eine xml datei speichert...

Hast du ein Codefragment/Beispiel?
 
Du hast doch sicher ein XML-Schema. Da musst du einfach den Typ des Feldes als base64Binary definieren.
Könnte so aussehen:
Code:
<complexType name="ContentType">
		<sequence>
			<element name="FileName" type="string"/>
			<element name="Content" type="base64Binary"/>
		</sequence>
	</complexType>
In der JavaKlasse die generiert wird, wird dann für Content ein byte-Array definiert. Dieses ist der Inhalt deiner Datei. Setzen wirst du das ganze dann ca. so müssen:
Code:
File f = new File("...");
byte[] b = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
fis.read(b);
fis.close();
//erzeugen der JAXB-Klasse, weiß nicht genau ob es dafür eine Factory oder was ähnliches gibt
ContentType contentType = new ContentType();
contentType.setFileName(f.getName());
contentType.setContent(b);

Das ist alles
 
Niki hat gesagt.:
Du hast doch sicher ein XML-Schema. Da musst du einfach den Typ des Feldes als base64Binary definieren.

Nein :lol:

Weiß jetzt nicht genau warum ich da ein Schemata entwerfen muss für ne xml datei für ein desktop app ?
 
Wie verwendest JaxB wenn du kein Schema hast?
Wodurch ist deine Datenstruktur beschrieben wenn nicht durch ein Schema?
Wie validierst du deine XML Dateien wenn nicht mit einem Schema?
 
Wildcard hat gesagt.:
Wie verwendest JaxB wenn du kein Schema hast?
Wodurch ist deine Datenstruktur beschrieben wenn nicht durch ein Schema?
Wie validierst du deine XML Dateien wenn nicht mit einem Schema?

Ich habe ein Klasse mit variablen +get/set Methoden. Erstelle davon ein objekt und speichere/lade eine xml datei
mit z.B.:

Code:
 JAXBContext jc = JAXBContext.newInstance(Person.class);
           Unmarshaller um = jc.createUnmarshaller ();           
	       Object o = um.unmarshal(new FileInputStream("daten.xml"));

was würde mir denn noch fehlen?
 
Ich habe noch nie gehört das man JaxB auch so benutzen kann (ich bin da sowieso eher der EMF Anhänger).
Normalerweise lässt du dir von JaxB die Java Klassen für ein bestehendes XML Schema erstellen.
 
Wildcard hat gesagt.:
Normalerweise lässt du dir von JaxB die Java Klassen für ein bestehendes XML Schema erstellen.

sorry war etwas außerhalb bin jetzt back :wink:

gibt es für das schema generien tools? Wie generiere ich ein JAXB Schema und dann anschließt die java Klassen? Ich habe meine .xml Datei nie validiert!?
 
Hier in diesem Beispiel:

wird einfach das schema books.xsd erzeugt und ur-plötzlich sind die attribute wie id,name,isbn da in der java klasse books ohne dass dies so irgendwo vorgegeben war, wie kann das sein???

quelle: http://java.sun.com/developer/technicalArticles/WebServices/jaxb/index.html

xjc.sh -p test.jaxb books.xsd -d work

The -p option identifies a package for the generated classes, and the -d option identifies a target directory. So for this command, the classes are packaged in test.jaxb within the work directory.

In response, the binding compiler generates a set of interfaces and a set of classes that implement the interfaces. Here are the interfaces it generates for the books.xsd schema:

* CollectionType.java. Represents the unnamed complex type for the <Collection> element.
* Collection.java. Represents the <Collection> element.
* BookType.java. Represents the BookType complex type.
* ObjectFactory.java. Contains methods for generating instances of the interfaces.

Here are the classes that implement the interfaces (these are generated in an impl subdirectory). Note that these classes are implementation-specific -- in this example, they are specific to the Reference Implementation. Because the classes are implementation-specific, classes generated by the binding compiler in one JAXB implementation will probably not work with another JAXB implementation. So if you change to another JAXB implementation, you should rebind the schema with the binding compiler provided by that implementation.

* impl/CollectionTypeImpl.java. Implements the CollectionType interface described in CollectionType.java.
* impl/CollectionImpl.java. Implements the Collection interface described in Collection.java.
* impl/BookTypeImpl.java. Implements the BookType interface described in BookType.java.

In total, the generated classes represent the entire books.xsd schema. Notice that the classes define get and setmethods that are used to respectively obtain and specify data for each type of element and attribute in the schema.

You then compile the generated interfaces and classes. For example:

javac test/jaxb/*.java test/jaxb/impl/*.java

This compiles all of the interfaces and classes in the test.jaxb package generated by the binding compiler.
Unmarshal the Document

Unmarshalling

Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document. The content tree is not a DOM-based tree. In fact, content trees produced through JAXB can be more efficient in terms of memory use than DOM-based trees.

The content objects are instances of the classes produced by the binding compiler. In addition to providing a binding compiler, a JAXB implementation must provide runtime APIs for JAXB-related operations such as marshalling. The APIs are provided as part of a binding framework. The binding framework comprises three packages. The primary package, javax.xml.bind, contains classes and interfaces for performing operations such as unmarshalling, marshalling, and validation (marshalling and validation will be covered later). A second package, javax.xml.bind.util, contains a number of utility classes. The third package, javax.xml.bind.helper, is designed for JAXB implementation providers.

To unmarshal an XML document, you:

* Create a JAXBContext object. This object provides the entry point to the JAXB API. When you create the object, you need to specify a context path. This is a list of one or more package names that contain interfaces generated by the binding compiler. By allowing multiple package names in the context path, JAXB allows you to unmarshal a combination of XML data elements that correspond to different schemas.

For example, the following code snippet creates a JAXBContext object whose context path is test.jaxb, the package that contains the interfaces generated for the books.xsd schema:

import javax.xml.bind.JAXBContext;

JAXBContext jc = JAXBContext.newInstance("test.jaxb");

* Create an Unmarshaller object. This object controls the process of unmarshalling. In particular, it contains methods that perform the actual unmarshalling operation. For example, the following code snippet creates an Unmarshaller object:

import javax.xml.bind.Unmarshaller;

Unmarshaller unmarshaller = jc.createUnmarshaller();

* Call the unmarshal method. This method does the actual unmarshalling of the XML document. For example, the following statement unmarshals the XML data in the books.xml file:

Collection collection= (Collection)
unmarshaller.unmarshal(new File( "books.xml"));

Note that a Collection here is a test.jaxb.Collection, not a java.util.Collection.


* Use the get methods in the schema-derived classes to access the XML data. Recall that the classes that a JAXB compiler generates for a schema include get and set methods you can use to respectively obtain and specify data for each type of element and attribute in the schema. For example, the following statement gets the data in the books and book elements:

CollectionType.BooksType booksType = collection.getBooks();
List bookList = booksType.getBook();

After obtaining the data, you can display it directly from your program. Here, for example, is a program that unmarshals the data in the books.xml file and then displays the data. If you run the program, you should see the following result:

Book details
Item id: 999
Book Name: Learning JAXB
Book ISBN: 123445
Book Price: 34 $
Book category: other
Book promotion: 10% on this book if purchased by March 2003
No of Authors 1
Author Name Jane Doe

Book details
Item id: 129
Book Name: Java Webservices today and Beyond
Book ISBN: 522965
Book Price: 29 $
Book category: magazine
Book promotion: Buy one get Learning webservices Part 1 free
No of Authors 2
Author Name John Brown
Author Name Peter T.
 
Es gibt bei den Bibliotheken Tools mit denen man sich eben aus einer xsd Datei die Java Klassen generieren kann.
Ich hab einmal eine Anleitung für XML und Java mittels XMLBeans geschrieben. Schau dir das halt einmal an: XMLBeans_Anleitung
 
Niki hat gesagt.:
Es gibt bei den Bibliotheken Tools mit denen man sich eben aus einer xsd Datei die Java Klassen generieren kann.
Ich hab einmal eine Anleitung für XML und Java mittels XMLBeans geschrieben. Schau dir das halt einmal an: XMLBeans_Anleitung

das ist klar... nur wie erstelle ich die .XSD Datei?
 
Eintippen ist ein heißer Tipp 🙂
irgendwas musst du schon machen, die Informationen kann sich das Programm ja nicht aus den Fingern saugen. Das XML-Schema beschreibt dir ja die Struktur deiner XML Objekte. Das ist daher der Einsprungspunkt.
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben