I need to log XML message.
I use this code:
//From object to xml
public String createMarshalerDealInfoType(DealInfoType dealInfoType) {
StringWriter contactStr = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DealInfoType.class);
Marshaller jaxbUnmarshaller = jaxbContext.createMarshaller();
contactStr = new StringWriter();
jaxbUnmarshaller.marshal(dealInfoType, contactStr);
} catch (JAXBException e) {
log.error(e.getMessage());
}
return contactStr.toString();
}
In test class:
ResponseType ResponseType = woNspDealWS.createRequestWS(DealRequestType);
String DealResponce = updateDealEsb.createMarshalerDealInfoType(ResponseType.getDealInfo());
log.debug("Response: \n " + DealResponce);
Problem: in log output I see only first line of responce, not whole message
18:01:42,975 DEBUG updateDeal_Test:73 - Response: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
How do I make to print all response in XML?
SOLVED:
resolved problem with use annotation #XmlRootElement for test class.
The object which you have passed in test class might be empty
ResponseType.getDealInfo()
For solving this problem need use annotation #XmlRootElement in test class
Related
I have to create an XML file from java object with specific format of my XML file.
My xml should look like this.
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person
name="Nick"
birthday = "09.03.1814"/>
</persons>
Or like this one.
<?xml version="1.0" encoding="UTF-8"?>
<persons>
<person surname="Sd" name="aaa" birthday = "09.03.1814"/>
</persons>
Depends on old format of xml. How could i do this?
Thanks!
I've try to use this code for creating xml
public static void createNewXml(List<Person> persons) {
List<DtoPerson> dtoPersonList = new ArrayList<>();
for (Person person : persons) {
dtoPersonList.add(new DtoPerson(person));
}
try {
JAXBContext jaxbContext = JAXBContext.newInstance(DtoPerson.class);
Marshaller marshaller = jaxbContext.createMarshaller();
File file = new File("personx.xml");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
for(DtoPerson personDto : dtoPersonList) {
marshaller.marshal(personDto, file);
}
} catch (JAXBException e) {
throw new RuntimeException(e);
}
System.out.println(Arrays.toString(dtoPersonList.toArray()));
}
But this code create an xml with other format
<dtoPerson>
<birthday>09.03.1814</birthday>
<name>aaa Sd</name>
</dtoPerson>
You need to add annotation #XmlAttribute to affected fields in DtoPerson.
I am not very familiar with Marshaller but I gave a try in a project and the XML was generated fine. But when I apply the XML validation (with the .xsd file) it shows an Error saying that <Timestamp> is empty. I debugged the application and I see that inside my Bean the Timestamp is not empty but when the Marshaller generates the XML it is really empty. In the other hand, other attributes which uses XMLGregorianCalendar are present in the XML. I don't know what is happening.
This is the XML generator function:
public void generateXMLReportFile(String fileOutputDirectory,
String xmlOutputFileName,
CRSOECD crsOECD)
throws JAXBException, FileNotFoundException, IOException
{
String encoding = "UTF-8";
// Schema location to write to generated XML file.
String schemaLocation = "urn:oecd:ties:crs:v1 CrsXML_v1.0.xsd";
// Generate The Report:
JAXBContext context = JAXBContext.newInstance(CRSOECD.class);
StringWriter xml = new StringWriter();
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
marshaller.marshal(crsOECD, xml);
try (OutputStream out = new FileOutputStream(fileOutputDirectory + xmlOutputFileName)) {
byte[] bytes = xml.toString().getBytes(encoding);
out.write(bytes, 0, bytes.length);
log.info("XML generated.");
}
}
If I debug, I see that the element which is empty in the XML is not empty in the Bean crsOECD.
Guys I just found the answer. The problem was that my element was tagged like this:
#XmlElement(name = "Timestamp", required = true)
#XmlSchemaType(name = "dateTime")
And I was passing only a Date with no Time.
I am getting the error as mentioned. Adding code snippet to get more about what am I doing. Please have a look and help. Thanks in advance.
My xml:
<?xml version="1.0" encoding="UTF-8"?>
<ClientConfigData>
<requestType>type1</requestType>
<refreshEnable>false</refreshEnable>
<compressionEnable>false</compressionEnable>
<transformationEnable>true</transformationEnable>
...
</ClientConfigData>
My Java:
#XmlRootElement
public class ClientConfigData {
private String requestType;
private boolean refreshEnable;
private boolean compressionEnable;
private boolean transformationEnable;
...
}
And here, I am creating java object from xml:
File configFile = new File(classLoader.getResource("ClientRegistration.xml").getFile());
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(ClientConfigData.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
ClientConfigData configData= (ClientConfigData) jaxbUnmarshaller.unmarshal(configFile);
System.out.println(configData);
} catch (JAXBException e) {
e.printStackTrace();
}
You should add a qualified root element name to the #XmlRootElement annotation. In your case it will be:
#XmlRootElement(name = "ClientConfigData")
By default JAXB searches for clientConfigData (with the lower-case first letter).
I am currently using the following code to marshal an object into an xml string
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
StringWriter writer = new StringWriter();
Marshaller marshaller = context.createMarshaller();
heartbeat.setHeader(header);
heartbeat.setHeartbeatEvent(event);
marshaller.marshal(heartbeat, writer);
String stringXML = writer.toString();
return stringXML;
} catch (JAXBException e) {
throw new RuntimeException("Problems generating XML in specified "
+ "encoding, underlying problem is " + e.getMessage(),
e);
}
Which produces the following header
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
My desired output is the following
<?xml version=\"1.0\"?>
By adding this to the marshaller
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
I receive
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?>
and changing the JAXB_FRAGMENT property to TRUE removes the header entirely. I have been following the JAXB - Remove 'standalone="yes"' from generated XML thread attempting to solve the problem but I have had no luck so far. Can someone please give me some insight on how to get my desired header from the JAXB marshaller?
When marshalling to an OutputStream using a combination of the following produces the expected output.
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
The problem you are seeing occurs when you marshal to a Writer, which appears to be a bug in the JAXB reference implementation. You can raise an issue at the link below:
https://java.net/jira/browse/JAXB/
You could always do:
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
StringWriter writer = new StringWriter();
writer.append("<?xml version=\"1.0\"?>");
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
heartbeat.setHeader(header);
heartbeat.setHeartbeatEvent(event);
marshaller.marshal(heartbeat, writer);
String stringXML = writer.toString();
return stringXML;
} catch (JAXBException e) {
throw new RuntimeException("Problems generating XML in specified "
+ "encoding, underlying problem is " + e.getMessage(),
e);
}
EclipseLink JAXB (MOXy) also supports the com.sun.xml.bind.xmlHeaders and it works correctly when marshalling to a Writer (I'm the MOXy lead)
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
This worked for me
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
In JAXB 3.0.1 the above mentioned constants cause a PropertyException:
jakarta.xml.bind.PropertyException: name: com.sun.xml.bind.xmlDeclaration value: false
In this case, the XML preamble can be configured with these marshaller constants:
marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
https://eclipse-ee4j.github.io/jaxb-ri/3.0.0/docs/ch05.html
I am currently using the following code to marshal an object into an xml string
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
StringWriter writer = new StringWriter();
Marshaller marshaller = context.createMarshaller();
heartbeat.setHeader(header);
heartbeat.setHeartbeatEvent(event);
marshaller.marshal(heartbeat, writer);
String stringXML = writer.toString();
return stringXML;
} catch (JAXBException e) {
throw new RuntimeException("Problems generating XML in specified "
+ "encoding, underlying problem is " + e.getMessage(),
e);
}
Which produces the following header
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
My desired output is the following
<?xml version=\"1.0\"?>
By adding this to the marshaller
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.FALSE);
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
I receive
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><?xml version="1.0"?>
and changing the JAXB_FRAGMENT property to TRUE removes the header entirely. I have been following the JAXB - Remove 'standalone="yes"' from generated XML thread attempting to solve the problem but I have had no luck so far. Can someone please give me some insight on how to get my desired header from the JAXB marshaller?
When marshalling to an OutputStream using a combination of the following produces the expected output.
marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\"?>");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
The problem you are seeing occurs when you marshal to a Writer, which appears to be a bug in the JAXB reference implementation. You can raise an issue at the link below:
https://java.net/jira/browse/JAXB/
You could always do:
JAXBContext context;
try {
context = JAXBContext.newInstance(heartbeat.getClass());
StringWriter writer = new StringWriter();
writer.append("<?xml version=\"1.0\"?>");
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
heartbeat.setHeader(header);
heartbeat.setHeartbeatEvent(event);
marshaller.marshal(heartbeat, writer);
String stringXML = writer.toString();
return stringXML;
} catch (JAXBException e) {
throw new RuntimeException("Problems generating XML in specified "
+ "encoding, underlying problem is " + e.getMessage(),
e);
}
EclipseLink JAXB (MOXy) also supports the com.sun.xml.bind.xmlHeaders and it works correctly when marshalling to a Writer (I'm the MOXy lead)
http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
This worked for me
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
In JAXB 3.0.1 the above mentioned constants cause a PropertyException:
jakarta.xml.bind.PropertyException: name: com.sun.xml.bind.xmlDeclaration value: false
In this case, the XML preamble can be configured with these marshaller constants:
marshaller.setProperty("org.glassfish.jaxb.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
https://eclipse-ee4j.github.io/jaxb-ri/3.0.0/docs/ch05.html