Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让jaxb在解组期间忽略某些数据_Java_Xml_Service_Jaxb - Fatal编程技术网

Java 如何让jaxb在解组期间忽略某些数据

Java 如何让jaxb在解组期间忽略某些数据,java,xml,service,jaxb,Java,Xml,Service,Jaxb,我有一个xml结构“Filter”,它被解包到一个名为“Filter”的java类中 XML状态大致如下所示: <filter> <propertyType> <propertyName>prop1</propertyName> <propertyValue>val1</propertyValue> </propertyType> <propertyType> &l

我有一个xml结构“Filter”,它被解包到一个名为“Filter”的java类中

XML状态大致如下所示:

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

建议1
瓦尔1
建议2
瓦尔2
通常,它工作得很好

但是,在某些情况下,其中一个属性值本身包含xml结构(请参见下面的第二个属性值):


建议1
瓦尔1
建议2
瓦尔布
这里的问题是,在解组此结构后,propertyValue为null

我只希望能够让解组忽略这段看起来像xml的代码,并将其视为一个简单的字符串值


有人知道我怎样才能做到这一点吗?谢谢你的回复

AFAIK JAXB致力于xml模式,以将xml解组为Java对象。所以,若模式将元素定义为简单元素,那个么它只能包含文本。如果需要将XML存储为简单文本。您可能需要使用CDATA构造对其进行转义。尝试按如下所示封装相同的XML,在解组处理后,您将获得原样的XML

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

建议1
瓦尔1
建议2
valB]]>

使用“@xmlanyement”的注释如何? 您可以获得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
}

对于这个用例,我将创建一个XSLT来转换XML文档。然后使用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>


糟糕,糟糕的建议。不要将可解析的数据视为未解析的数据。@Alejandro:样式表为本例生成了正确的结果,尽管我同意可能有更好的编写方法。我回答的重点是,可以使用JAXB和XSLT的组合来实现期望的结果。
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();
    }

}
<?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>