Génération d'un format précis du fichier XML
Bonjour,
je voudrais créer un fichier XML avec un Java.
je suis arriver à faire ce fichier XML
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
<?xml version="1.0" encoding="UTF-8"?>
<graphml>
<key1 key1="x" />
<key2 key2="y" />
<key3 key3="name" />
<graph1>
<Node1>
<X>40</X>
<Y>50</Y>
<Name>MASG</Name>
</Node1>
<Node2>
<X>30</X>
<Y>40</Y>
<Name>MASG</Name>
</Node2>
<edge>
<target>n1</target>
<source>n2</source>
<Name>FH</Name>
</edge>
</graph1>
</graphml> |
avec le code java suivant :
Code:
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 119 120 121 122 123 124 125
|
import java.sql.SQLException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
public class test {
// public Element setChild (Connection conn, int ID_Child) throws NotFoundException, SQLException{
//
//
// Graphic_positionDao graphic_position_X_dao = new Graphic_positionDao();
// Graphic_positionDao graphic_position_Y_dao = new Graphic_positionDao();
// Graphic_positionDao graphic_position_ID_MASG_dao = new Graphic_positionDao();
// String x = graphic_position_X_dao.getObject(conn, ID_Child).getX_MASG_CSG();
// String y = graphic_position_Y_dao.getObject(conn, ID_Child).getX_MASG_CSG();
// String ID_MASG = graphic_position_ID_MASG_dao.getObject(conn, ID_Child).getID_MASG_CSG();
// Element child = new Element(ID_MASG);
// child.addContent(new Element("X").setText(x));
// child.addContent(new Element("Y").setText(y));
// return child;
// }
public static void main(String[] args) {
//
// <rows>
// <row>
// <firstname>Alice</firstname>
// <lastname>Starbuzz</lastname>
// <address>Sunset Read</address>
// </row>
// </row>
//
// Port_typeDao port_type_dao = new Port_typeDao();
Document document = new Document();
Element root = new Element("graphml");
Element key1 = new Element("key1");
Element key2 = new Element("key2");
Element key3 = new Element("key3");
Element child1 = new Element("graph1");
//
// Creating a child for the root element. Here we can see how to
// set the text of an xml element.
//
key1.setAttribute("key1", "x");
key2.setAttribute("key2", "y");
key3.setAttribute("key3", "name");
// key1.addContent(null);
Element child2 = new Element("Node1");
child2.addContent(new Element("X").setText("40"));
child2.addContent(new Element("Y").setText("50"));
child2.addContent(new Element("Name").setText("MASG"));
Element child3 = new Element("Node2");
//child3.setAttribute("key", "y");
child3.addContent(new Element("X").setText("30"));
child3.addContent(new Element("Y").setText("40"));
child3.addContent(new Element("Name").setText("MASG"));
Element child4 = new Element("edge");
//child4.setAttribute("key", "name");
child4.addContent(new Element("target").setText("n1"));
child4.addContent(new Element("source").setText("n2"));
child4.addContent(new Element("Name").setText("FH"));
//
// Add the child to the root element and add the root element as
// the document content.
//
root.setContent(key1);
root.addContent(key2);
root.addContent(key3);
root.addContent(child1);
child1.addContent(child2);
child1.addContent(child3);
child1.addContent(child4);
document.setContent(root);
try {
FileWriter writer = new FileWriter("Graph2.xml");
XMLOutputter outputter = new XMLOutputter();
//
// Set the XLMOutputter to pretty formatter. This formatter
// use the TextMode.TRIM, which mean it will remove the
// trailing white-spaces of both side (left and right)
//
outputter.setFormat(Format.getPrettyFormat());
//
// Write the document to a file and also display it on the
// screen through System.out.
//
outputter.output(document, writer);
outputter.output(document, System.out);
} catch (IOException e) {
e.printStackTrace();
}
}
} |
mais je voudrais que la structure de mon ficheir XML généré soit différente, en l'occurence comme ceci :
Code:
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
|
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns/graphml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns/graphml">
<key id="name" for="node">
<default></default>
</key>
<key id="y" for="node">
<default>0</default>
</key>
<key id="x" for="node">
<default>0</default>
</key>
<key id="name" for="edge">
<default></default>
</key>
<graph edgedefault="directed">
<node id="M23">
<data key="name">M23</data>
<data key="y">154.0</data>
<data key="x">34.0</data>
</node>
<node id="M1">
<data key="name">M1</data>
<data key="y">37.0</data>
<data key="x">175.0</data>
</node>
<node id="node5">
<data key="name">node5</data>
<data key="y">20.0</data>
<data key="x">36.0</data>
</node>
<edge source="node5" target="M1"/>
<edge source="node5" target="M23"/>
<edge source="M23" target="M1">
<data key="name">N0</data>
</edge>
</graph>
</graphml> |
que doit je changer et que dois je ajouter ?!!!