File objekt in xml speichern?

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?
 

Niki

Top Contributor
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.
 
J

JaireMichel

Gast
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?
 

Niki

Top Contributor
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
 
J

JaireMichel

Gast
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 ?
 

Wildcard

Top Contributor
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?
 
J

JaireMichel

Gast
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?
 

Wildcard

Top Contributor
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.
 
J

JaireMichel

Gast
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!?
 
J

JaireMichel

Gast
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.
 

Niki

Top Contributor
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
 
J

JaireMichel

Gast
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?
 

Niki

Top Contributor
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.
Ähnliche Java Themen
  Titel Forum Antworten Datum
Q XML-File als Objekt in Java XML & JSON 6
E JAXB und java.nio.file.Path XML & JSON 4
Trèfle Formatierung v. JSON File XML & JSON 7
L login - Java IOException jaas.config File or Directory not present XML & JSON 1
G Probleme mit Jsoup in .jar File XML & JSON 11
K JAR-File mit XML-Datei XML & JSON 1
JBoby Yaml File lesen und in Objekte umwandel (SnakeYAML / YamlBeans) XML & JSON 0
O XML Zugriff auf Entity File XML & JSON 0
B xsd-Datei ins jar file XML & JSON 5
S XLSX-File NoSuchMethodError XML & JSON 7
M SEPA XML file XML & JSON 11
S Xml File für den Aufbau einer anderen Xml Datei XML & JSON 6
S XML file lässt sich in Eclipse nicht erstellen XML & JSON 2
M <root> node in bestehendes XML-File XML & JSON 8
jstei001 .xsd File wärend der Laufzeit einlesen und XMl rausschreiben XML & JSON 5
L Datenstruktur in XML-File schreiben XML & JSON 2
G DXF-File schreiben XML & JSON 17
GUI-Programmer java.io.File aus .jar beziehen XML & JSON 6
B XML file erstellen und in lesbarer form speichern XML & JSON 2
M XML File generieren XML & JSON 3
S Aus XML-File eine Baumstruktur erzeugen XML & JSON 5
N XML File aus Internet korrekt mit absätzen formatiert abspeichern XML & JSON 10
J xmlParser/-Writer: DTD in seperates File auslagern XML & JSON 4
C Xml file gegen eine DTD validieren XML & JSON 3
G Problem beim schreiben von XML in eine File XML & JSON 2
S Premature end of file XML & JSON 7
T File öffnen XML & JSON 6
B XML file für schtasks XML & JSON 5
G Simples XML File einlesen mit JDom. Prefix not bound ? XML & JSON 2
G Xml File schematisch auslesen XML & JSON 2
G XMLEncoder: discarding statement LinkedList.add(File) XML & JSON 16
X XML File auslesen/parsen u. in ne Hashmap speichern XML & JSON 2
M Unhierarchisches XML-File mit DOM-Parser auswerten XML & JSON 5
M XML File während Applet-Start auslesen? XML & JSON 10
X Einzelne Tags rausnehmen aus einem großen XML-file XML & JSON 4
E XOM setzen von XML-Schema declaration beim erzeugen XML-File XML & JSON 2
M XSL-File für Transformation verwenden XML & JSON 3
M XML-File verabeiten XML & JSON 19
A XML-File auslesen, alles leer? XML & JSON 2
S Geändertes XML-File wieder abspeichern. XML & JSON 10
D XML-file mit Eclipse erstellen + Namespaces XML & JSON 2
A html-File nach txt-File konvertieren XML & JSON 15
K XML to file geht nicht :( XML & JSON 4
A Internetinhalte auslesen und in einem XML-File speichern XML & JSON 4
T select-statement aus mysql in ein xml-file umwandeln XML & JSON 3
J XML File updaten XML & JSON 2
A XML-file an der Konsole ausgeben XML & JSON 4
D In ein XML File meinStyleSheet.css eintragen? XML & JSON 9
J Client für WebService programmieren (aus WSDL-File) XML & JSON 15
R wie kann ich ein xml file in einen string einlesen? XML & JSON 2
M *.group() in File auslesen XML & JSON 4
A XML-File ausgeben XML & JSON 2
W aus XML-File gelesenes als Methodenaufruf verwenden XML & JSON 5
T Elemente aus XML-File löschen (JDom) XML & JSON 9
T [JDOM] XML File, neue Daten hinzufügen XML & JSON 5
D Aus Java XML-File an PHP-Skript senden: Encodingproblem? XML & JSON 3
T Prob: Auslesen XML File mit JDOM XML & JSON 2
B DOM oder HTML File drucken "wie ein Browser es zeigen w XML & JSON 3
B Json Objekt sinnvoll plätten? XML & JSON 1
I ID von Referenz speichern, nicht ganzes Objekt XML & JSON 1
M Objekt zu jsonArray in .json datei hinzufügen ? XML & JSON 3
M Großes Json Objekt benutzen XML & JSON 5
B Wie kann man das ecncoding in einem vorhandenen Document-Objekt ändern? XML & JSON 2
Q Konvertierung von json zum Java Objekt nach vorgegebenem Schema XML & JSON 3
N XStream ConversionException beim Deserialisieren in (Hibernate)Objekt XML & JSON 6
Spin XML - Objekt hinzufügen XML & JSON 3
L Objekt Serialisierung: Schreiben aller Attribute erzwingen XML & JSON 5
F Objekt-Generator aus XML und XSD XML & JSON 9
M Objekt mit DOM serialisieren XML & JSON 6
F Marshaling eines JAXB Objektes worin ein anderes JAXB Objekt eingeschlossen ist XML & JSON 6
G PDF (iText) mit Objekt weiterarbeiten XML & JSON 10
M XML-Datum als Referenz auf Objekt-Variable auslesen XML & JSON 5
M Xlink in Java-Objekt umwandeln XML & JSON 9
M XML Encoder Objekt speichern, obwohl BufferedImage im Objekt XML & JSON 3
S XML Datei speichern nicht möglich XML & JSON 13
B Xml speichern - müssen alle Klassen deklariert werden? XML & JSON 12
M Layout + Inhalt einer JTable speichern XML & JSON 30
I Liste in YAML Datei speichern und wieder auslesen XML & JSON 1
E JTreedaten in eine XML Datei speichern XML & JSON 3
T Pfad in einer Variablen speichern XML & JSON 1
T Formulardaten in XML Datei speichern XML & JSON 5
E ResultSet als XML Datei speichern XML & JSON 7
R sax, stax, jdom, jaxb? List von Objekten speichern und laden XML & JSON 6
A XML datei lesbar speichern (eingerückt) XML & JSON 7
R XLM Datei lesen Ändern (kompliziertes Search and Replace) und Speichern XML & JSON 12
T Daten in Xml speichern XML & JSON 4
T Daten aus Programm in XML Datei speichern XML & JSON 2
M RSS als String speichern XML & JSON 12
F XML mit DOM einlesen- Teile in Arraylist speichern XML & JSON 4
T RSS-Feed parsen und in Datenbank speichern XML & JSON 6
P Variablen in XML speichern XML & JSON 3
L XML Daten auslesen und in Tabelle (Array) speichern XML & JSON 1
M Arraylist als xml speichern/ laden XML & JSON 2
J Logdaten als XML speichern und lesen XML & JSON 2
T Wie einen String(XML Inhalt) in eine XML Datei speichern? XML & JSON 8
J Tabelleninhalte in XML- speichern und laden XML & JSON 5
P XML Dateien zusammenführen und in eine XML speichern XML & JSON 7
M Speichern größerer Datenmengen // XML vs. Serializing . XML & JSON 5
D Mit Sax Inhalt zwischen Tags in Vector speichern XML & JSON 4
A Jetzt mal im Klartext. XML speichern XML & JSON 2

Ähnliche Java Themen

Neue Themen


Oben