Bonjour

Jai donc un probleme lors de la creation dun fichier XML avec DOM, mes valeurs sont bien ajoutees au bon endroit mais il ne respecte pas l'indentation.

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
 
 
public void addFile(String xmlFile) throws ParserConfigurationException, SAXException, IOException, TransformerException{
		File file = new File(xmlFile);
		if(file.exists()){
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = factory.newDocumentBuilder();
	        Document doc = builder.parse(xmlFile);
			Element root= doc.getDocumentElement();
			System.out.println("ROOT "+root.getNodeName());
 
			// to get the name of a child of the root
			Element nodeParent =doc.createElement("account"); // in this case account
			root.appendChild(nodeParent);
 
			// to read the HashMap
			Iterator i = parser.getContent().keySet().iterator();
			while (i.hasNext())
			{
 
				String key = (String)i.next();
				String value = (String)parser.getContent().get(key);
				// If it is a key (so the key contains = ) we add in the XML file	
				if (key.contains("=")){
 
					String keymodif=key.substring(0,key.length()-1);
					Element nodechild =doc.createElement(keymodif);
					nodechild.setTextContent(value);
 
					nodeParent.appendChild(nodechild);
 
 
				}
			}
			 File f = new File(nameFile);
			 StreamResult result=new StreamResult(f);
			 DOMSource source =new DOMSource(doc);
			 TransformerFactory transformerFactory = TransformerFactory.newInstance();
			 Transformer transformer = transformerFactory.newTransformer();
			 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
			 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
			 transformer.transform(source,result);
 
		}
	}
Resultat
Code XML : 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
 
<accounts>
  <account>
    <LogonID>jsmith</LogonID>
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
    <Phone>Smith</Phone>
  </account>
  <account>
    <LogonID>xcjc</LogonID>
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
    <Phone>123-456-78</Phone>
  </account>
<account>
<LogonID>jdsdsdsc</LogonID>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<Phone>123-456-78</Phone>
</account>
</accounts>


Ps pas tres visible ici, desole mais en fait je perds l'indentation "XML" pour mes valeurs ajoutees

Pouvez vous m'aider