I would like to deserialize an XML subtree as string (with Jackson) in JAVA:
input structure:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<nodeA>text</nodeA>
<nodeB>
<nodeS>
<nodeS1>...</nodeS1>
<nodeS2>...</nodeS2>
</nodeS>
</nodeB>
</root>
into something like this:
public class Pojo {
#JacksonXmlProperty(localName="nodeA")
private String nodeA; // = "text"
#JacksonXmlProperty(localName="nodeB")
#JsonDeserialize(using = MyXmlDeserializer.class)
private String nodeB; // = "<nodeS><nodeS1>...</nodeS1><nodeS2>...</nodeS2></nodeS>"
}
The node nodeS should be taken as "raw-value" without ANY modifications to the xml and put it into a String class member.
I've tried it with custom deserializer or as #JacksonRawValue with no avail.
if one just could access the raw value of the "currentNode", that would help a lot.
Any alternative (jackson-related) solutions welcome :-)
It's kind of a workaround rather than a solution, but it's possible to use XmlMapper inside your JsonDeserializer:
xmlMapper.writeValueAsString(jsonNode);
As a result you'll get Xml string again. But as I wrote, it's workaround and I don't like it gets from Xml to Json, and from Json to Xml again.
If anyone get a better solution (using Jackson), please share.
Related
I have a huge XML response from an external end point. I want to parse the XML response to java classes. I was able to parse into respective POJOS if none of the XML had namespaces and things went well till that point.
However, the response might contain namespaces only at the root element.
For eg, like this
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="somevalue here"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RestOfTheDocument> something here </RestOfTheDocument>
</Document>
I can guarantee that none of the inner xml elements will have any more namespaces.
Is there a way to implement this? I saw a few answers ignoring namespaces completely but dont feel very convincing. Is there a way to properly parse these.
This is my java class to model the XML response
#XmlRootElement(name = "Document")
static class Response {
#XmlElement(name = "RestOfTheDocument", required = true, nillable = false)
RestOfTheDocument restOfTheDocument;
}
what I tried ?
Add namespace information to the #XmlRootElement like this
#XmlRootElement(name = "Document", namespace = "somevalue here")
Doing so is making all the inner xml elements to be NULL.
NOTE: I have abstracted the huge inner level to be RestOfTheDocument but there are lot many but none of them will have any namespaces whatsoever in the response!
Thank you!
SimpleXml will ignore the namespaces and just parse the XML:
public class Document {
public String RestOfTheDocument;
}
final String data = ...;
final SimpleXml simple = new SimpleXml();
final Document document = simple.fromXml(data, Document.class);
System.out.println(document.RestOfTheDocument);
This will output:
something here
From maven central:
<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.4.0</version>
</dependency>
I have problem deserializing following xml:
<root>
<apples>
<apple>
<id>1</id>
<weight>0.6</id>
</apple>
<apple>
<id>2</id>
<weight>0.7</id>
</apple>
</apples>
</root>
to java:
public class Root {
private List<Fruits> fruits;
}
In above xml source other variants may be eg oranges/orange etc. also since this is propertary xml I cannot change it's schema. By default I am using #JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT) and #JsonSubTypes({/*fruits subtypes listed*/}). After few unsuccessful attempts I am not sure what is the proper mapping/configuration to solve the problem.
Out of the box, Jackson is a JSON parser, not XML.
There are extensions for Jackson that allow for XML conversion. See http://wiki.fasterxml.com/JacksonExtensionXmlDataBinding for more info there. But it seems as though a pure XML parser may be better for this case.
Perhaps the javax.xml.stream.* packages.
i.e. http://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html for more info.
How would one unmarshall the following XML response with JAXB into a domain class:
<?xml version="1.0" encoding="UTF-8"?>
<time>2014-01-14T06:24:34+00:00</time>
My first instinct was to use something like (short version):
#XmlRootElement
public class Time {
#XmlElement
public Date time;
}
but I think JAXB then sees 2 elements with the Time name. I also tried without using the #XmlRootElement annotation, but to no avail.
Have you tried using #XmlValue instead of #XmlElement for the time field? After all, it is the value of the root element, rather than a sub-element.
I've now tried this with the file supplied, and it works properly.
I'm using SimpleXml to (de)serialize POJOs. Now, I have a big XML which has some elements which are not needed. For instance, with this XML:
<Root>
<Element>Used</Element>
<Another>Not used</Another>
<Root>
I want to create a POJO which looks like:
#Root
class Root{
#Element
private String element;
}
Problem is that I'm getting this Exception:
simpleframework.xml.core.ElementException: Element 'Another' does not have a
match in class blah.blah.Blah at line 1
So... how should I configure the POJO so that I can parse the XML correctly?
Set strict to false within the Root annotation to ignore any XML elements or attributes that do not appear in the class.
#Root(strict=false)
Alternatively, set strict to false when you read the xml in the serialiser:
Root root = serializer.read(Root.class, source, false);
you can add (required=false) to a single element
#Element(required=false)
private int statusCode;
if you have more elements use
#Root(strict=false)
What i need to do is the following. i need using json, to serialize my object to xml as follows:
<employee>
<name>Name</name>
<id>the_database_id</id>
<employee>
my java code/bean is as follows:
public class PairPOJO<K,V> implements IPair<K,V> {
private K first;
private V second;
...
}
for reasons irrelevant for my problem, firtst/second need to remain with these names...
so the serialization produces
<employee>
<first>Name</first>
<second>the_database_id</second>
<employee>
I am new to json, is there any bean annotation or any other way letting me to accomplish what i need to do? any site, example, info is highly appreciated.
My code snippet is...
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;
private String serializeItem(Object obj) {
JSON jsonObject = JSONSerializer.toJSON(obj);
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setTypeHintsEnabled(true);
return xmlSerializer.write(json);
}
Eh? You want to use JSON to serialize a java object to XML? That makes no sense. – skaffman 2 hours ago
and
Can you explain why you want to use JSON? It's just a common data format and as such seems like an additional unnecessary translation to go from POJO->JSON->XML. Why not just go POJO->XML?
Is there any better way for me to serialize to xml from complex objects? As i said it is the first time i need to do something similar and got over web that this is the best/better way to do it.
The general idea is that i need to return through REST ws beans.