【问题标题】:How to edit an XML file with XStream?如何使用 XStream 编辑 XML 文件?
【发布时间】:2014-12-25 11:11:47
【问题描述】:

请在下面找到我的 xml 示例:

<product>
<shoes brand="Nike" price="58 Euros" size="43" />
</product>

我能够读取 xml 并将品牌、价格和尺寸的值放入 3 个输入文本中。

现在我有了输入文本中的值,我希望能够修改它们并生成一个新的 xml 文件。我遇到的问题是,当我生成带有 inputextarea 的对话框中显示的新 xml 时,它仍然是初始值。请在下面找到我的完整代码:

index.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

<h:head>
    <meta charset="utf-8" />
    <title>XStream</title>
</h:head>
<h:body id="mybody">

    <h:panelGrid columns="2" cellpadding="5" id="mongrid">
        <h:outputLabel for="brand" value="Brand :" />
        <p:inputText id="brand" value="#{xmlreader.product.shoes.brand}"/>

        <h:outputLabel for="size" value="Size :" />
        <p:inputText  id="size" value="#{xmlreader.product.shoes.size}"/>

        <h:outputLabel for="price" value="Price :" />
        <p:inputText id="price" value="#{xmlreader.product.shoes.price}"/>

        <p:commandButton value="Generate XML" actionListener="#      {xmlreader.testDataModelGeneration()}" />
    </h:panelGrid>


     <h:form id="readxml" enctype="multipart/form-data">

            <p:fileUpload id="upload" 
                          mode="advanced" 
                          required="true"  
                          cancelLabel="Cancel XML"
                          style="margin-top: 15px;"
                          requiredMessage="At least one file needs to be selected"
                          allowTypes="/(\.|\/)(xml)$/"
                          invalidFileMessage="Not allowed file type" 
                          invalidSizeMessage="The file is too large (500 kb max)" 
                          uploadLabel="Process XML"
                          fileLimit="1"
                          fileLimitMessage="Please load one file at a time"
                          dragDropSupport="true"
                          label="Select XML" 
                          multiple="false"
                          fileUploadListener="#{xmlreader.allocation}"
                          sizeLimit="500000"
                          update=":mongrid"
                          />
     </h:form>

    <p:dialog id="dialogId"
              draggable="false" 
              dynamic="true" 
              header="XML Generated"
              widgetVar="dlgxml"
              width="1115"
              height="650"
              closable="true"
              modal="true"
              ><!-- Affichage du XML -->

        <h:form id="xml">

            <p:inputTextarea value="#{xmlreader.xmlFinal}" 
                             cols="115"
                             autoResize="true"
                             rows="20"
                             />

        </h:form>
    </p:dialog>

</h:body>

类产品:

@XStreamAlias("product")
public class Product implements Serializable{

private Shoes shoes;

public Product() {
}

public Product(String className) {
    shoes = new Shoes();
}

public Shoes getShoes() {
    return shoes;
}

public void setShoes(Shoes shoes) {
    this.shoes = shoes;
}

}

类鞋:

@XStreamAlias("shoes")
public class Shoes implements Serializable{

@XStreamAsAttribute
private String brand;

@XStreamAsAttribute
private String price;

@XStreamAsAttribute
private String size;

public Shoes() {
}

public Shoes(String brand, String price, String size) {
    this.brand = brand;
    this.price = price;
    this.size = size;
}

public String getBrand() {
    return brand;
}

public void setBrand(String brand) {
    this.brand = brand;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getSize() {
    return size;
}

public void setSize(String size) {
    this.size = size;
}

}

我的豆子:

@SessionScoped

@ManagedBean(name="xmlreader") 公共类 XmlReader 实现 Serializable{

private Product product;
private Shoes shoes;

private String xmlFinal;
private StringBuilder xml;

@PostConstruct
public void init() {
    this.product = new Product();
    this.shoes = new Shoes();
    this.xml = new StringBuilder();
}

/*read an existing xml file and complete the input text*/
public void allocation(FileUploadEvent event) throws IOException{

    this.xml = new StringBuilder();

    try {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(event.getFile().getInputstream(), "UTF-8"))) {
            String line;

            while ((line = br.readLine()) != null) {
                this.xml.append(line);
               // System.out.println(line);

            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }

    try {
        XStream xstream = new XStream();

        xstream.processAnnotations(Product.class);
        xstream.autodetectAnnotations(true);
        Product resultat = (Product) xstream.fromXML(this.xml.toString());
        this.product = resultat;   

    } catch (Exception e) {
        e.printStackTrace();
    }

}

/*Generate a new xml displayed in a dialog*/
public void testDataModelGeneration(){

    StringBuilder xml = new StringBuilder();
    this.xmlFinal = new String();

    XStream xstream = new XStream();
    xstream.autodetectAnnotations(true);

    String xml2 = xstream.toXML(this.product);

    xml.append(xml2);
    this.xmlFinal = xml.toString();
    RequestContext.getCurrentInstance().execute("PF('dlgxml').show();");
    //this.xmlFinal = xml.toString();

}

public Product getProduct() {
    return product;
}

public void setProduct(Product product) {
    this.product = product;
}

public Shoes getShoes() {
    return shoes;
}

public void setShoes(Shoes shoes) {
    this.shoes = shoes;
}

public String getXmlFinal() {
    return xmlFinal;
}

public void setXmlFinal(String xmlFinal) {
    this.xmlFinal = xmlFinal;
}

public StringBuilder getXml() {
    return xml;
}

public void setXml(StringBuilder xml) {
    this.xml = xml;
}

}

亲切的问候

【问题讨论】:

    标签: xml jsf serialization primefaces xstream


    【解决方案1】:

    您需要刷新对话框内容才能显示 Backing Bean 的最后更改。

    RequestContext.getCurrentInstance().update(":dialogId:contentId");
    RequestContext.getCurrentInstance().execute("PF('dlgxml').show();");
    

    在 XHtML 中,如果您使用按钮显示对话框,则它需要具有如下更新属性update=":dialogId:contentId"

    【讨论】:

    • 您好 Proverbio,感谢您的回复。但是我不能让它工作。我确实有一个按钮来显示对话框,但内容 id 是什么?是 inputtextarea 的 id 吗?
    • 内容 ID 是您在对话框中拥有的表单 ID。
    • 我试过了,但还是不行。我不确定我的问题是与对话框更新有关还是与 XStream 有关。
    • 我正在尝试使用 2 个不同的 bean(一个用于读取 xml 并完成输入文本,一个用于修改并生成新的 xml),但仍然没有成功。
    【解决方案2】:

    要更新对话框中的输入文本值,您需要做的就是:

    1) 将输入文本放入表单中。 2) 在 fileUpload 中,通过它的 id 更新这个表单。

    非常感谢我的同事。

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多