I want to marshall a java object to xml string. Therefore I have been given a schema from which I generate the JAXB classes. There is a set method(corresponding to an element of
NEW ANSWER
In your sample code val is the hexBinary representation of str.getBytes(). But the value you are setting on the arr property is the bytes from the hex encoded String.
String str = new String("hi");
String val = Hex.encodeHexString(str.getBytes());
root.setString(str);
root.setArr(val.getBytes());
I believe what you mean to do is the following:
String str = new String("hi");
String val = Hex.encodeHexString(str.getBytes());
root.setString(str);
root.setArr(str.getBytes());
Which will produce the following output
val=6869
getString =hi
getArr=hi
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xsi:schemaLocation="http://www.example.com/schema.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<string>hi</string>
<a>6869</a>
</root>
Put another way
String str = new String("hi");
System.out.print("String: " + str);
System.out.println(" hexBinary: " + Hex.encodeHexString(str.getBytes()));
String val = Hex.encodeHexString(str.getBytes());
System.out.print("String: " + val);
System.out.println(" hexBinary: " + Hex.encodeHexString(val.getBytes()));
Will output:
String: hi hexBinary: 6869
String: 6869 hexBinary: 36383639
ORIGINAL ANSWER
I have not been able to reproduce the issue that you are seeing. I am using a String size of 500000. Below is what I have tried (does this sample work for you?). Is it possible that the truncation is due to the console you are writing the long String to?
Demo
package forum12146217;
import java.io.*;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
// Build a long String
StringBuilder stringBuilder = new StringBuilder();
for(int x=0; x<500000; x++) {
stringBuilder.append("a");
}
Root root = new Root();
root.setString(stringBuilder.toString());
System.out.println(root.getString().length());
// Marshal the object to a StringWriter
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.example.com/schema.xsd");
StringWriter stringWriter = new StringWriter();
marshaller.marshal(root, stringWriter);
// Convert StringWriter to String
String xml = stringWriter.toString();
//System.out.println(xml);
// Unmarshal the XML and check length of long String
Unmarshaller unmarshaller = jc.createUnmarshaller();
Root unmarshalledRoot = (Root) unmarshaller.unmarshal(new StringReader(xml));
System.out.println(unmarshalledRoot.getString().length());
}
}
Root
package forum12146217;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Root {
private String string;
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
}
Output
500000
500000