I used the api jgraph
below the content of the xml file and the java code but I did not get the result that I expect
I was not able to insert a picture corresponding to the graph that I desire to draw
this is the content of my xml file
="1.0"="ISO-8859-1"
- <annotation>
- <concepts>
<concept>Person</concept>
<concept>Actor</concept>
</concepts>
- <triplets>
- <triplet>
- <subject>
<value>Willard Christopher</value>
<instanceOf>Person</instanceOf>
</subject>
<predicate>equivalent</predicate>
- <object>
<value>Will" Smith Jr</value>
<instanceOf>Actor</instanceOf>
</object>
</triplet>
</triplets>
</annotation>
this my code java :
import java.awt.BorderLayout;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.view.mxGraph;
public class Final {
public static Object valSubject =null;
public static Object valInstance =null;
public Final () {}
public JFrame frame;
private static final long serialVersionUID = -8123406571694511514L;
public void dessin(String filename) throws SAXException, IOException,
ParserConfigurationException {
frame = new JFrame();
JPanel contentPane;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 320);
frame.setVisible(true);
frame.setBounds(100, 100, 550, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
mxGraph graph = new mxGraph();
Object parent = graph.getDefaultParent();
graph.getModel().beginUpdate();
File nomf = new File(filename);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(nomf);
doc.getDocumentElement().normalize();
try {
double h = 80;
double l = 80;
Element root=doc.getDocumentElement();
while(root.hasChildNodes())
{
NodeList concepts = doc.getElementsByTagName("concepts") ;
NodeList triplets = doc.getElementsByTagName("triplet");
for (int i = 0; i < concepts.getLength(); i++)
{
Node nodeC=concepts.item(i);
Element elementC =(Element) nodeC ;
Object composantconcept=graph.insertVertex(parent,null, concepts.item(i).getNodeValue(), h,l,
20,20,mxConstants.STYLE_SHAPE + "="
+ mxConstants.SHAPE_ELLIPSE);
String s=getValue("concept",elementC);
for (int j = 0; j < triplets.getLength(); j++)
{
Node node = triplets.item(j);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element elementT = (Element) node;
if (triplets.item(j).equals("subject"))
{
Object valsubject = graph.insertVertex(parent, null, getValue("value", elementT)
,h, l,
20, 20);
if (triplets.item(j).equals("instanceOf"))
{ if (triplets.item(j).getNodeValue()== s ){
graph.insertEdge(parent, null, null,valsubject ,composantconcept);
}}
if (triplets.item(j).equals("object"))
{
Object valobject = graph.insertVertex(parent, null, getValue("value", elementT),
h, l,
20, 20);
if (triplets.item(j).equals("instanceOf"))
{ if (triplets.item(j).getNodeValue()== s ){
graph.insertEdge(parent, null, null,valobject ,composantconcept);
}}
if( triplets.item(j).equals("instanceOf") )
{
Object rectangle =graph.insertVertex(parent, null,
getValue("value", elementT),
h, l, 80, 30);
if(triplets.item(j).getNodeValue()==s){
graph.insertEdge(parent,null, null, rectangle, composantconcept);
}}
graph.insertEdge(parent, null, getValue("predicate", elementT),valsubject, valobject);
}}
l = l + 60;
h = h + 60;
}}}}}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
graph.getModel().endUpdate();
}
mxGraphComponent graphComponent = new mxGraphComponent(graph);
frame.add(graphComponent);}
private static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
public static void main(String[] args) throws SAXException, IOException,
ParserConfigurationException {
System.out.print("bonjour");
Final frame = new Final();
frame.dessin("C:/Users/Hela/Desktop/copie.xml");
}}