Please find below my xml example :
<product>
<shoes brand="Nike" price="58 Euros" size="43" />
</product>
I am able to read the xml and to put the value of brand, price and size in 3 inputtext.
Now that I have the values in the the inputtext, I would like to be able to modify them and to generate a new xml file. The problem that I have is that when I generate the new xml displayed in a dialog with a inputextarea, it is still the initial values. Please find below my the full code :
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>
class Product :
@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;
}
}
class 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;
}
}
My the bean :
@SessionScoped
@ManagedBean(name="xmlreader") public class XmlReader implements 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;
}
}
Kind Regards
To update the input text values in the dialog, all you need to do is :
1) Put the inputtext in a form. 2) In the fileUpload, update this form by its id.
Many thanks to my colleague.