2011-02-25 49 views
3

我有一个xml结构“Filter”,它被解组到一个名为“Filter”的java类中。如何让jaxb在解组过程中忽略某些数据

的XML状态看起来大致是:

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

通常情况下,它的伟大工程。

然而,有些情况下这些属性值本身的一个包含XML结构(见下文第二的PropertyValue)某些情况下:

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

这里的问题是,解编这种结构后,是的PropertyValue空。

我想简单地能够unmarshalling忽略这个XML看起来代码,并将其视为一个简单的字符串值。

有谁知道我该如何做到这一点?谢谢你的回复!

回答

0

AFAIK JAXB在xml模式下工作,将XML解编组为Java对象。因此,如果模式将元素定义为简单元素,则它只能包含文本。如果您需要将XML存储为简单文本。您可能需要使用CDATA构造来转义它。尝试使用下图所示的相同方法,在不加掩饰的情况下,您将获得XML。

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

坏,坏的意见。不要将可分析数据视为未分析的数据。 – 2011-03-03 14:43:43

1

如何使用“@XmlAnyElement”的注释? 你可以得到org.w3c.dom.Element的实例。 应该可以通过操作这个实例来获取文本数据。

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

获取文本数据的示例。

// 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; 
    } 
} 
+0

通过大家的意见解决了我的问题。感谢你的支持。 – TimC 2011-03-08 17:50:39

1

对于这种用例,我将创建一个将转换XML文档的XSLT。然后,使用javax.xml.transform的*的API,将XML转换到JAXBResult解组对象:

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> 
+0

我不认为这个样式表会做你想要的... – 2011-03-03 14:46:59

+0

@Alejandro:样式表为这个例子产生了正确的结果,尽管我同意可能有更好的方法来编写它。我的答案的焦点是可以使用JAXB和XSLT的组合来实现预期的结果。 – 2011-03-03 15:03:48

相关问题