在JSF中读取和写入XML文件

Tin*_*iny 2 xml jsf jsf-2.2

我在网上没有发现如何在JSF中读/写XML文档.我在JSP中使用XALAN了解JSTL .例如,

下面定义了以下XML文件/WEB-INF.

<?xml version="1.0" encoding="UTF-8"?>

<fruits>
    <fruit>
        <name>Orange</name>
        <price>10</price>
    </fruit>

    <fruit>
        <name>Banana</name>
        <price>20</price>
    </fruit>

    <fruit>
        <name>Apple</name>
        <price>30</price>
    </fruit>
</fruits>
Run Code Online (Sandbox Code Playgroud)

可以在JSP中读取此文档,如下所示.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<c:import var="items" url="/WEB-INF/TextXML.xml"/>
<x:parse var="fruits" doc="${items}"/>

<table rules="all" border="1">
    <tr>
        <th>index</th>
        <th>Fruit Name</th>
        <th>Price</th>
    </tr>

    <x:forEach var="item" select="$fruits/fruits/fruit" varStatus="loop">
        <tr>
            <td><c:out value="${loop.index+1}"/></td>
            <td><x:out select="$item/name" /></td>
            <td><x:out select="$item/price" /></td>
        </tr>
    </x:forEach>
</table>
Run Code Online (Sandbox Code Playgroud)

这将填充具有三列的HTML表.

如何在JSF中实现相同的功能,可能使用JAXB或其他东西?

Bal*_*usC 5

你确实可以使用JAXB.

我们假设您已经有一个javabean代表<fruit>.您甚至可以为此重用现有的JPA实体.

public class Fruit {

    private String name;
    private BigDecimal price;

    // Add/generate getters+setters.
}
Run Code Online (Sandbox Code Playgroud)

(注意,JavaBean类和属性名必须完全匹配XML元素的名称<fruit>,<name>并且<price>,否则,你需要一个@XmlElement(name="actualXmlElementName")在任的那些)

现在,创建另一个javabean表示<fruits>,纯粹用于JAXB(它需要一个@XmlRootElement表示XML根元素的类,即使XML文档基本上只包含一个实体列表).

@XmlRootElement
public class Fruits { // Classname must match <fruits>, otherwise set it as @XmlRootElement(name="fruits")

    @XmlElement(name="fruit") // Name must thus match <fruit>. We could also make the property name "fruit" so that we can omit @XmlElement, but a getFruit() method returning a list of fruits isn't self-documenting.
    private List<Fruit> list;

    public Fruits() {
        // Keep default c'tor alive.
    }

    public Fruits(List<Fruit> list) {
        this.list = list;
    }

    public List<Fruit> getList() { 
        return list;
    }

}
Run Code Online (Sandbox Code Playgroud)

至于读/写,您可以在JSF中完美地阅读/WEB-INF如下:

InputStream input = externalContext.getResourceAsStream("/WEB-INF/fruits.xml");
Run Code Online (Sandbox Code Playgroud)

但写作是一个独特的故事.您不应该将文件写入部署空间.由于显而易见的原因,无论何时重新部署WAR,所有更改都将丢失,因此不打算将其作为永久存储位置.您需要将其写入部署空间之外的某个固定路径.在下面的例子中,我假设文件被移动到/var/webapp/fruits.xml服务器的磁盘文件系统上.

因此,您可以使用JAXB在托管bean中读取和写入XML文件,如下所示:

@Named
@RequestScoped
public class Bean {

    private File file = new File("/var/webapp/fruits.xml");
    private JAXBContext jaxb; // Can be application scoped. It's thread safe.
    private List<Fruit> fruits;

    @PostConstruct
    public void init() throws JAXBException, IOException {
        jaxb = JAXBContext.newInstance(Fruits.class);
        fruits = ((Fruits) jaxb.createUnmarshaller().unmarshal(file)).getList();
    }

    public void save() throws JAXBException {
        jaxb.createMarshaller().marshal(new Fruits(fruits), file);
    }

    public List<Fruit> getFruits() { 
        return fruits;
    }

}
Run Code Online (Sandbox Code Playgroud)

您可以<h:dataTable>通常的方式在JSF中显示它以进行编辑.

<h:dataTable value="#{bean.fruits}" var="fruit">
    <h:column><h:inputText value="#{fruit.name}" /></h:column>
    <h:column><h:inputText value="#{fruit.price}" /></h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)