Reputation: 27
I try to transform a string into an array of byte (byte[]) and save it in a xml file. my problems are different values of the byte[] after marshal und unmarshal the object with jaxb.
I´m sorry for the format of my posting!
`
@XmlRootElement
public class Token {
private byte[] token;
public void createToken(){
String stringTest = "ABCDEF";
this.token = stringTest.getBytes(Charset.forName("UTF-8"));
}
public byte[] getToken() {
return token;
}
public void setToken(byte[] token) {
this.token = token;
}
}// ENDE CLASS TOKEN
@XmlRootElement(namespace = "TokenNS")
public class TokenCollection {
private List<byte[]> collection = new ArrayList<>();
public void addToken(byte[] tokenIn){
this.collection.add(tokenIn);
}
@XmlElement( name = "TokenCollection")
public List<byte[]> getTokenCollection(){
return this.collection;
}
public void test(){
Token t = new Token();
t.createToken();
byte[] tmp = t.getToken();
this.addToken(tmp);
}
}// ENDE TOKENCOLLECTION
STARTER:
public Starter() {
Path path = Paths.get("trivial.xml");
tc.test();
JAXB.marshal(tc, System.out);
try (Writer out = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
JAXB.marshal(tc, out);
} catch (IOException io) {
io.printStackTrace();
}
}
`
OUTPUT: Should be: 65 66 67 68 69 70
XML-FILE (with wrong values):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:tokenCollection xmlns:ns2="TokenNS">
<TokenCollection>QUJDREVG</TokenCollection>
</ns2:tokenCollection>
Upvotes: 2
Views: 9806
Reputation: 149037
A JAXB (JSR-222) implementation will represent byte[]
in XML as the base64Binary
schema type. If you want an alternate representation you can use an XmlAdapter
.
Upvotes: 4
Reputation: 418167
Now after you edited your post, it works perfectly.
I read the XML with the following code, and in the end it prints the same "ABCDEF"
string:
Path path = Paths.get("trivial.xml");
TokenCollection tc = JAXB.unmarshal(path.toFile(), TokenCollection.class);
// Prints "ABCDEF"
System.out.println(new String(tc.getTokenCollection().get(0),
StandardCharsets.UTF_8));
Upvotes: 0