I am using XStream to serialize Java objects to XML. Is it possible to customize XStream so that when it serializes an object it inserts an attribute in the root XML element?
Let's say I have
class A{
int foo = 1;
}
I want XStream to serialize instances of A to look like:
<A type="text/xml">
<foo>1</foo>
</A>
Where the attribute text/xml is automatically added to the root element.
My use case is serializing my java object and insert it as the content element inside Atom entry documents. The end result would look like:
<feed>
<content type="text/xml">
<foo>1</foo>
</content>
</feed>
I do not require being able to unmarshall the feed. I need a generic solution that is agnostic to the class of the object I am serializing.
Can I achieve this with XStream?
The only way are the the XStream.useAttributeFor(...) methods.
This would force you to configure XStream for each object type you are using though, thus not agnostic.
So I don't think XStream is the tool you need.
Related
I would like to use JAXB to read and write only a few parts of a very large XML. I would like to be able to do this without having to define a root object class for every element and attribute in the XML. The example below outlines what I need:
I have the XML
<A>
<B/>
<C/>
<D/>
</A>
I would like to use JAXB to get two functions
public String getC() {
...
return C
}
public void writeC(String C) {
... // replaces C value with the paramter C inside the XML
}
Without having to define a new class A with the annotations for B, C, and D.
How can I do this with JAXB? Is there a faster / more efficient way to achieve what I am trying to do than JAXB or a simple File Reader and Writer?
The purpose of this is to use a GUI to load and edit config settings that are stored in an XML file. Thank you.
I managed to solve this by using StAX instead. It offered complete flexibility for me to pick out which tags and which attrbitues I needed.
There is a project called EclipseLink MOXy that could solve this type of problem using JAXB. It allows you to map an existing java bean to or from xml and define exceptions from default JAXB using xml or json for the mapping description.
I have an application that reads and writes data from/to XML files to/from a DB using JAXB. The "human readability" of these XML files is one of the top priorities in this project as the idea is that XML files from different DBs can be compared to each other.
Now, I have some tables with attributes that hold XML strings themselves which need to be included to my JAXB objects. As I don't have control over these XML strings and readability is key, I helped myself so far with an XmlAdapter that pretty prints the XML from the DB and wraps it in a CDATA element.
The problem of this solution is that the XML string looks a bit lost within the CDATA element and smart text editors with syntax highlighting don't recognize the XML, so they don't highlight the syntax of that XML.
I was wondering therefore if there's a way to "embed" this XML within an element of my JAXB model as if it would be part of the model. So, I need a kind of XmlAdapter that would parse an XML from a String field of my JAXB class and somehow pass the parsing events to the underlying XMLWriter.
I've spent a lot of time looking for a solution combining JAXB and Stax but didn't succeed. In my view I would need some hook exactly between the JAXB and the Stax layer, so that I can customize the events that JAXB sends to the Stax Writers.
Example:
#XmlRootElement
public class MyJaxbModel {
#XmlAttribute
private Integer anAttribute;
#XmlElement
private String xml;
public MyJaxbModel(Integer anAttribute, String xml) {
this.anAttribute = anAttribute;
this.xml = xml;
}
...
}
Then marshalling the following instance of this class
new MyJaxbModel(999, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><element1><child1>bla</child1><child2>ble</child2></element1>");
should result in the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<MyJaxbModel anAttribute="999">
<xml>
<element1>
<child1>bla</child1>
<child2>ble</child2>
</element1>
</xml>
</MyJaxbModel>
Obviously I need it to work also the other way round, i. e. unmarshalling this XML would return me the MyJaxbModel object including the XML string in the xml field.
I have a central xml configuration in an Commons project. This means in this project I don´t know which xml elements in the config exists. I want to hold the root element in the configuration. The requestet configuration item from other project has to be with a class object. This class object contains neccessary information of the requestet xml Element with the jaxb annotation. Is it possible to get a filled object from JAXB with this guidelines?
I have no code examples because I don´t know yet how to start.
JAXB mapps the XML structure into Object oriented graph/hierarchy.
If you don't know the structure/hierarchy of XML, you should use push/pull parsing SAX, StAX, ...
i have a certain class hierarchic which is transformed to XML using JAXB.
i want the created XML to have the xsi:noNamespaceSchemaLocation and xmls:xsi attributes in the head element ( xsi:noNamespaceSchemaLocation="Something.xsd" xmls:xsi="http://www.w3.org/2001/XMLSchema-instance").
i dont want JAXB do use the schema in any way - just store these attributes and the values.
is there a specific annotation for this? i could create just a constant XmlAttribute but it seems wrong...
thanks
You can set the JAXB_NO_NAMESPACE_SCHEMA_LOCATION on the Marshaller like the following:
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "address.xsd");
We have an XML that needs to be converted to an object and vice versa. Something like Xstream does. Until now we were using Xstream to marshall and unmarshall the object/xml.
However the problem is that an object that corresponds to XML in xstream, needs to have all the tags as attributes; else if XML contains any extra tags which are not present in object; it bombs.
Or, we need to have custom convertors written to make sure that the operation goes as desired. I was also suggested that common digester allows Xpath parsing from XML to an object.
I am wondering what is the best approach; as long as:
I just want to convert XML to Object and vice versa.
Have the ability to silently ignore any fields in XML that map not be present in mapping object.
What do you suggest?
You need to use a custom MapperWrapper as documented here http://pvoss.wordpress.com/2009/01/08/xstream/
XStream xstream = new XStream() {
#Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MapperWrapper(next) {
#Override
public boolean shouldSerializeMember(Class definedIn,
String fieldName) {
if (definedIn == Object.class) {
return false;
}
return super.shouldSerializeMember(definedIn, fieldName);
}
};
}
};
The only thing it does is tell XStream to ignore all fields that it does not know to deal with.
You might want to take a look at this question...
What is the best way to convert a java object to xml with open source apis
These are some of the libraries that it lists...
http://simple.sourceforge.net/
http://x-stream.github.io/
http://xmlbeans.apache.org/
http://www.rgagnon.com/javadetails/java-0470.html
I would suggest using http://simple.sourceforge.net/ I uses annotations to map attributes and elements and has a "non strict" mode which enables you to read from the XML document ignoring all attributes and elements not present in the Java object.