How to make jaxb ignore certain data during teardown

I have a Filter xml structure that gets unmarshalled into a java class called Filter.

The XML state looks something like this:

<filter> <propertyType> <propertyName>prop1</propertyName> <propertyValue>val1</propertyValue> </propertyType> <propertyType> <propertyName>prop2</propertyName> <propertyValue>val2</propertyValue> </propertyType> </filter> 

It usually works great.

However, there are certain situations when one of these property values ​​contains an xml structure (see the second value property below):

 <filter> <propertyType> <propertyName>prop1</propertyName> <propertyValue>val1</propertyValue> </propertyType> <propertyType> <propertyName>prop2</propertyName> <propertyValue><nodeA><nodeB>valB</nodeB></nodeA></propertyValue> </propertyType> </filter> 

The problem is that after parsing this structure, the Value property is null.

I would just like to be able to unmarshalling to ignore this xml-looking code and treat it as a simple string value.

Does anyone know how I can do this? Thanks for any answer!

+3
source share
3 answers

What about annotation using "@XmlAnyElement"? You can get an instance of org.w3c.dom.Element. Text data must be obtained when working with this instance.

 class PropertyType { private String propertyName; // private String propertyValue; // comment out @XmlAnyElement(lax=true) private List<org.w3c.dom.Element> propertyValue; // Adding this } 

exsample to get text data.

 // It is assumed that the child node is one. org.w3c.dom.Node nd = propertyValue.get(0).getFirstChild(); while(true) { if (nd.hasChildNodes()) { nd = nd.getFirstChild(); } else { System.out.println(nd.getNodeValue()); // this is text data break; } } 
+1
source

In this case, I will create an XSLT that converts the XML document. Then using javax.xml.transform. * API, convert XML to JAXBResult to unmount an object:

 import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.util.JAXBResult; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; public class Demo { public static void main(String[] args) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); File xsltFile = new File("transform.xsl"); StreamSource xsltSource = new StreamSource(xsltFile); Transformer transformer = tf.newTransformer(xsltSource); File xml = new File("input.xml"); StreamSource xmlSource = new StreamSource(xml); JAXBContext jc = JAXBContext.newInstance(Filter.class); JAXBResult jaxbResult = new JAXBResult(jc); transformer.transform(xmlSource, jaxbResult); Filter filter = (Filter) jaxbResult.getResult(); } } 

transform.xsl

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*" /> </xsl:copy> </xsl:template> <xsl:template match="propertyValue"> <xsl:value-of select="descendents"/> <xsl:element name="propertyValue"> <xsl:value-of select="node()"/> </xsl:element> </xsl:template> </xsl:stylesheet> 
+1
source

AFAIK JAXB is working on an xml schema for unmarshalling XML into a Java object. Therefore, if a schema defines an element as a simple element, it can only contain text. If you need to store XML as plain text. You may need to avoid this using the CDATA construct. Try to include the same as shown below, after disassembling, you will get XML as is.

 <filter> <propertyType> <propertyName>prop1</propertyName> <propertyValue>val1</propertyValue> </propertyType> <propertyType> <propertyName>prop2</propertyName> <propertyValue><![CDATA[<nodeA><nodeB>valB</nodeB></nodeA>]]></propertyValue> </propertyType> </filter> 
0
source

All Articles