I am producing a XML file by the help of JAXB.
public String getPrices() {
StringWriter writer = new StringWriter();
JAXBContext context;
try {
context = JAXBContext.newInstance(AllMerchandises.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(this.allMerchandises, writer);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return writer.toString();
}
the resulting XML String looks like this.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:merchandises xmlns:ns2="example/test/workspace">
<merchandise id="1">
<name>Cat</name>
<price>50</price>
</merchandise>
<merchandise id="2">
<name>Dog</name>
<price>100</price>
</merchandise>
<merchandise id="3">
<name>Ape</name>
<price>150</price>
</merchandise>
<merchandise id="4">
<name>Gorilla</name>
<price>200</price>
</merchandise>
<merchandise id="5">
<name>Elephant</name>
<price>250</price>
</merchandise>
</ns2:merchandises>
It is my task that the document could be showed a little bit designed in a browser by using XSLT. Therefor I need to include the .xsl in my XML file. Is there a maybe a way to tell the JAXB marshaller to make a inclusion? What could I do otherwise?
I need to include sth like this as 2nd line:
<?xml-stylesheet type="text/xsl" href="myfile.xsl"?>
thanks for every good idea
Finally I ended up using the same solution as described in this thread.
Making JAXB generate an XML processing instruction
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 have a xml file where I would like to fill in values from another xml file. For example, if I have data.xml:
<Data>
<Person>
<Name>neby</Name>
<Phone>
<Home>5553456789</Home>
<Mobile>5559879876</Mobile>
</Phone>
</Person>
</Data>
I want to fill in test.xml like this (Data taken from the above file):
<Test>
<Name>${Name}</Name>
<Number>${Home}</Number>
</Test>
In my Java program, I create XML files during runtime. I would like to give a file name, "pass" it to test.xml, have test.xml read it, and fill in the text.
Is there a way to do this?
Am I doing it correctly? All I get is the encoding. Never mind, I got it to work. Using the data.xml from above.
MyXSL.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="Data/Person">
<Test>
<Name><xsl:value-of select="Name"/></Name>
<Number><xsl:value-of select="Home"/></Number>
</Test>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Code:
try {
File stylesheet = new File("MyXSL.xsl");
File dataFile = new File("data.xml");
Document document = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(dataFile);
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
System.out.println(stringWriter.toString());
} catch(Exception e){
e.printStackTrace();
}
Output: <?xml version="1.0" encoding="UTF-8"?>
What am I doing wrong? Above code works.
Create XSLT and then something like that (very simple from oracle java tuts):
// ...
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
// ...
public class Stylizer {
// ...
public static void main (String argv[]) {
// ...
try {
File stylesheet = new File(argv[0]);
File datafile = new File(argv[1]);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(datafile);
// ...
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = Factory.newTransformer(stylesource);
}
}
}
All informations and step-by-step guide to create XSLT and this sample app is here.
Apache CXF/JAXB is not unmarshaling Japanese characters. If we are printing the xml using System.out.println output is coming properly like below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1470">
<designation>Eng</designation>
<name>マデュ</name>
<salary>20000.0</salary>
</employee>
If we are passing the same XML to CXF layer it is converting like below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1470">
<designation>Eng</designation>
<name>???</name>
<salary>20000.0</salary>
</employee>
How to solve this issue. Thanks in advance.
Can you try use PrintWriter on top of StringWriter?
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Can you try with XMLEventWriter to System.out?
XMLEventFactory events = XMLEventFactory.newInstance();
QName bar = new QName("urn:bar", "bar");
XMLOutputFactory factory = XMLOutputFactory.newInstance();
factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
XMLEventWriter writer = factory.createXMLEventWriter(System.out);
JAXBContext pContext = JAXBContext.newInstance(target);
Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(pObject, writer);
writer.add(events.createStartDocument());
writer.setDefaultNamespace("urn:bar");
writer.add(events.createStartElement(bar, null, null));
writer.add(events.createEndDocument());
writer.flush();
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