如何使用JAXB解析webflow

时间:2012-02-01 13:34:05

标签: java xml parsing annotations jaxb

我使用Spring webflow,我需要阅读流程定义 这个流程定义是一个xml文件...... 我想用JAXB解析这个XML:

<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/webflow
        http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd" 
        parent="global-flow" >

    <subflow-state id="aaaa" subflow="test/test19/aaaa">
        <transition on="torna" to="verificaPef" />
        <transition on="prosegui" to="bbbb" />
    </subflow-state>

    <subflow-state id="bbbb" subflow="test/test19/bbbb">
        <transition on="torna" to="aaaa" />
        <transition on="prosegui" to="end" />
    </subflow-state>

    <subflow-state id="cccc" subflow="common/cccc/page">
        <transition on="torna" to="bbbbb" />
    </subflow-state>
</flow> 

我尝试这种映射:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "flow", namespace = "http://www.springframework.org/schema/webflow")
public class Flow {

  @XmlElement(name = "subflow-state")
  private List<SubFlow> subFlowList;

  @XmlAttribute
  private String parent;

...

@XmlAccessorType(XmlAccessType.FIELD)
public class SubFlow {

  @XmlAttribute(name="id")
  private String id;

  @XmlAttribute(name="subflow")
  private String subflow;

但是不行!!! subFlowList列表总是为空(null) 怎么了?

1 个答案:

答案 0 :(得分:0)

您可以使用@XmlSchema注释来设置命名空间,使用elementFormDefault来控制命名空间限定。

<强>包信息

您可以通过在与域类相同的包中添加名为package-info的类来完成此操作。

@XmlSchema(
    namespace = "http://www.springframework.org/schema/webflow",
    elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

<强>流量

这将使您能够删除以前添加的其他命名空间信息。

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Flow {

  @XmlElement(name = "subflow-state")
  private List<SubFlow> subFlowList;

  @XmlAttribute
  private String parent;

  ...

}

了解更多信息