Bonjour,
Je veux modifier la valeur d'un attribut d'un élément (partnerLink) dans un fichier XML. la modification se passe bien mais le problème c'est que l'ordre des attributs change ainsi que les éléments se trouvant avant l'élément redondant a la contrainte l'attribut en question est supprimée. Voici le code:
Le fichier de référence voir pièces jointes.
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 import java.io.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import javax.xml.parsers.*; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.*; public class UpdateBPEL { static Document document; static org.jdom.Element racine; static void lireFichier(String fichier) throws Exception { File file = new File(fichier); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); document = db.parse(file); // System.out.println("Root element : " +document.getDocumentElement().getNodeName()); } static void updateElement(String element,String element1) { String ch1,ch2,ch3 =""; ch1="http://"+ element; ch2="http://"+ element1; long l; NodeList nodeLst = document.getElementsByTagName("partnerLink"); for (int s = 0; s < nodeLst.getLength(); s++) { //import Node fstNode = nodeLst.item(s); if (fstNode.getNodeType() == Node.ELEMENT_NODE) { Element fstElmnt = (Element) fstNode; //System.out.println(" element : " +fstElmnt.getAttributes().getLength()); l = fstElmnt.getAttributes().getLength(); // System.out.println("lol "+l); for (int h = 0; h <l; h++) { //import Node fNode= fstElmnt.getAttributes().item(h); if (fNode.getNodeType() == Node.ATTRIBUTE_NODE) { Attr fElmnt = (Attr) fNode; ch3 = fElmnt.getValue(); if (ch3.equals(ch1)){ // System.out.println("Result est "+ch3+"fElmnt.getName()=>"+fElmnt.getName()+"ch2 "+ch2); fstElmnt.setAttribute(fElmnt.getName(), ch2); } } } } } } public static void writeXmlFile(Document doc, String filename) { try { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file File file = new File(filename); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); } catch (TransformerConfigurationException e) { } catch (TransformerException e) { } } public static void main(String[] args) { try { lireFichier("E:\\Workspace\\FunctionProcesstest.bpel"); updateElement("AdderService.wsdl","AdderService1.wsdl"); writeXmlFile(document,"E:\\Workspace\\FunctionProcesstest2.bpel"); } catch (Exception e) { e.printStackTrace(); } } }
Je ne sais ce problème est du a quoi exactement. Merci d'avance pour votre aide
Partager