如何使用XStream编辑XML文件?

问题描述:

请找我下面的XML例子:如何使用XStream编辑XML文件?

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

我能够读取XML并把品牌,价格和值大小在3 inputText的。

现在我已经在输入文本中的值,我想能够修改它们并生成一个新的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; 
} 

} 

亲切的问候

要在对话框中更新输入文本值,所有你需要做的是:

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

非常感谢我的同事。

您需要刷新该对话框的内容,以显示最后修改从支持豆。

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

到XHTML中,如果你使用一个按钮来显示对话框,它需要有一个更新的属性作为波纹管update=":dialogId:contentId"

+0

Hello Proverbio,谢谢你的回复。然而,我无法让它工作。我确实有一个按钮来显示对话框,但是什么是内容ID?它是inputtextarea的ID吗? – Benusko 2014-10-30 09:07:49

+0

经过多次尝试,它没有解决问题。 – Benusko 2014-10-31 14:22:19

+0

内容ID是对话框中的表单ID。 – Proverbio 2014-11-01 20:21:11