Jaxb XML marshaling ISO20022 - jaxb

I try to generate (java 6) xml file which should have as a root:
<Document xmlns=”urn:iso:std:iso:20022:tech:xsd:pain.001.001.03”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
I have package-info is like below
#XmlSchema(
namespace="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03",
elementFormDefault=XmlNsForm.QUALIFIED,
xmlns={
#XmlNs(prefix="", namespaceURI="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03"),
#XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance")
}
)
I get xml file like this
<ns2:Document xmlns:ns2="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03" xmlns:ns3="inner">
<ns2:CstmrCdtTrfInitn>
<ns2:GrpHdr>..........
..............
</ns2:CstmrCdtTrfInitn>
</ns2:Document>
I have no idea what is wrong.

Related

JAXB how to deal with the header of XML response

I want to map the xml response to JAVA class, but how should I deal with the header?
<?xmlversion="1.0"encoding="UTF-8"standalone="yes"?>
<ns: someProperty xmlns: ns="http://example.namespace.com">
<ns: errorMsg>OK</ns: errorMsg>
<ns: list>
<ns: version>777</ns: version>
</ns: list>
</ns: someProperty>

Use custom deserializer in jackson fasterxml

I have an xml file like:
<users>
<user>
<name>User</name>
<details>
<age>15</age>
...
</details>
</user>
...
</users>
I want to map this xml to List, where User class defined as:
class User {
public String userName;
public int userAge;
}
I don't want to define mapping using annotations and so on. Just want to create an custom deserializer. I know that It is possible in Jackson for json to define custom deserializer by extending the JsonDeserializer.
Is there similar possibility for xml deserialization?

nodejs xml2j how do I skipPrefix

I have read the documentation but I can't quite seem to get the skipPrefix to work with xml2js. What I would like do to do is given the following xml remove the namespace prefix.
<root>
<part:tire>A</part:tire>
</root>
I would like the json object to exclude "part:".
Thanks
From the source code
prefixMatch = new RegExp(/(?!xmlns)^.*:/);
...
exports.stripPrefix = function(str) {
return str.replace(prefixMatch, '');
};
i think your xml string need to have proper namespace in order for that feature to work.

JAXB MOXy Binder issue when using #XmlAnyElement annotation

I have an issue when using the Binder implementation in MOXy.
Here is the input XML document (input.xml)
<?xml version="1.0" encoding="utf-8"?>
<root>
<unmapped />
</root>
And now, here is the source code used to unmarshal XML into a Binder instance and then update the XML from the corresponding Java object:
JAXBContext context = JAXBContext.newInstance(Input.class);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
Document document = builder.parse(new File("input.xml"));
Binder<Node> binder = context.createBinder(Node.class);
Input input = (Input) binder.unmarshal(document);
binder.updateXML(input);
In the end, the very simple Input class file:
#XmlRootElement(name = "root")
public class Input {
#XmlAnyElement
protected Object[] elements;
}
When the updateXML() method is invoked, the following exception is thrown:
java.lang.NullPointerException
at org.eclipse.persistence.internal.jaxb.DomHandlerConverter.convertObjectValueToDataValue(DomHandlerConverter.java:97)
We have been able to confirm this issue and it looks like it will be a very quick fix. You can use the link below to track our progress on this issue.
http://bugs.eclipse.org/391237
UPDATE
A fix has been checked into the EclipseLink 2.5.0 stream, a nightly download can be obtained from the following location:
http://www.eclipse.org/eclipselink/downloads/nightly.php
We have also checked in a fix to the EclipseLink 2.4.2 stream. A nightly download can be obtained from the above location starting October 12, 2012.

How Do I Use JAXB To Marshal Inherited Classes To XML?

I have been provided with an xsd that I compiled to Java classes using JAXB.
The generated classes create an abstract class called "Event" and several classes that extend it.
e.g. "DerivedEvent"
I am using the following to marshal it to XML.
ObjectFactory objectFactory = new ObjectFactory();
DerivedEvent derivedEvent = objectFactory.createDerivedEvent();
JAXBContext context = JAXBContextImpl.newInstance("com.my.root.namespace");
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
Class partialClass = Event.class;
QName partNamespace = new QName(Event.class.getSimpleName());
Object element = new JAXBElement(partNamespace, partialClass, derivedEvent);
// Create a stringWriter to hold the XML
StringWriter stringWriter = new StringWriter();
marshaller.marshal(element, stringWriter);
String xml = stringWriter.toString();
This then outputs the wrong root element. i.e.
<Event xsi:type="DerivedEvent" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DerivedStartPeriod xsi:nil="true"/>
<DerivedEndPeriod xsi:nil="true"/>
</Event>
instead of
<DerivedEvent>
<DerivedStartPeriod xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<DerivedEndPeriod xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</DerivedEvent>
If I set the partialClass and partNamespace to DerivedEvent it outputs the correct information.
However, I can't really do that as this marshalling is in a flow where the event could be one of 50 different derived events.
I can't really change the xsd to have substitution groups as mentioned here http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html as this change is likely to be lost when I am provided with a new version of the xsd.
I need to use the partial class and namespace as the Events are not root elements.
Can I add the events to be XmlRootElements with bindings somehow?
Or is there another way to address this?
I eventually found this page that helped me add the XmlRootElement:
https://codereview.stackexchange.com/questions/1877/jaxb-xjc-code-generation-adding-xmlrootelement-and-joda-datetime
Which I got working (dependencies/build.xml etc) by using the Annotate sample here:
http://confluence.highsource.org/display/J2B/Home
Creating a JAXBElement through generated ObjectFactory, or use proper QName.
This is bad:
QName partNamespace = new QName(Event.class.getSimpleName());
It should be like this
QName partNamespace = new QName("your namespace", "DerivedEvent");
You should find qname like this in ObjectFactory probably as constant...
ObjectFactory probably contains method createDerivedEvent with one argument of type DerivedEvent. It returns JAXBElement which can be marshaled to xml.

Resources