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
|
public static final Namespace GRAPH_NS = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns/graphml");
public void setFileXML (Element child1, Element child2, Element Edge){
final Namespace XSI_NS = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Document document = new Document();
// Element root = new Element("graphml");
// Crée un élément dans un namespace
Element root = new Element("graphml", GRAPH_NS);
root.setAttribute("schemaLocation","http://graphml.graphdrawing.org/xmlns/graphml", XSI_NS);
Element key1 = new Element("key");
Element key2 = new Element("key");
Element key3 = new Element("key");
key1.setAttribute("id", "x").setAttribute("for", "node");
key1.addContent(new Element("default").setText("0"));
key2.setAttribute("id", "y").setAttribute("for", "node");
key2.addContent(new Element("default").setText("0"));
key3.setAttribute("id", "name").setAttribute("for", "node");
key3.addContent(new Element("default").setText(" "));
Element child0 = new Element("graph");
child0.setAttribute("edgedefault", "directed");
//
// 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(child0);
child0.addContent(child1);
child0.addContent(child2);
document.setContent(root);
try {
FileWriter writer = new FileWriter("Graph1.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();
}
} |
Partager