Bonjour,

L'objectif de mon projet et de charger un XML, compléter des input text avec les valeurs lues dans le XML afin de pouvoir les modifier, et generér un nouveau fichier XML ainsi modifié (actuellement pas de génération mais affichage dans une fenetre de dialog). J'utilise JSF et Primefaces et actuellement je peux lire un XML, compléter les input text, mais lorsque je veux générer le nouveau xml avec les modifications, soit la fenêtre de dialog est vide, soit elle affiche les anciennes valeurs. Merci de bien vouloir trouver ci après mon code :

index :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<!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="#{xmlwriter.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"
                  showEffect="size" 
                  ><!-- Affichage du XML -->
 
            <h:form id="xml">
 
                <p:inputTextarea value="#{xmlreader.xmlFinal}" 
                                 id="input"
                                 cols="115"
                                 autoResize="true"
                                 rows="20"
                                 />
 
            </h:form>
        </p:dialog>
 
    </h:body>
</html>
Product :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@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;
    }
 
}
Shoes :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@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;
    }
 
}
Bean pour lire le XML :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@SessionScoped
@ManagedBean(name="xmlreader")
public class XmlReader implements Serializable{
 
    private Product product;
    private Shoes shoes;
 
    private String xmlFinal;
    private StringBuilder xml;
 
 
    /*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();
        }
 
    }
 
 
    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;
    }
 
}
Bean pour essayer de modifier et afficher le nouveau XML :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@SessionScoped
@ManagedBean(name="xmlwriter")
public class XmlWriter implements Serializable{
 
    private Product product;
    private Shoes shoes;
 
    private String xmlFinal;
    private StringBuilder xml;
 
 
    public XmlWriter() {
    }
 
   @PostConstruct
    public void init() {
        this.product = new Product();
        this.shoes = new Shoes();
        this.xml = new StringBuilder();
    }
 
    /*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;
    }
 
}
Si un développeur sait m'aider ce serait sympa, j'essai de trouver une solution depuis mercredi dernier.

Cordialement.