Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have a xml file and I need to go through this file and extract the data to draw a graph
please help me it's urgent
the important thing is to get the graph
if someone has a similar code..thank you for sending it me
please help me
Posted
Comments
Richard MacCutchan 30-Apr-14 10:14am    
What is the content of the XML, what are the dimensions of the graph axes, where do you expect to draw the graph, &c.? Please add proper detail to your question or search Google for Java graph libraries. And please don't say it's urgent as that means nothing here.
Member 10743868 30-Apr-14 11:10am    
this is an example xml file that I want to represent :

- <gatedocument version="2">
- <triplet>
<subject>Brad Pitt
<predicate>is</predicate>
<object>actor</object>
</triplet>
- <triplet>
<subject>Brad Pitt
<predicate>is</predicate>
<object>producer</object>
</triplet>
- <triplet>
<subject>Pitt
<predicate>nominatedFor</predicate>
<object>Academy Award</object>
</triplet>
- <triplet>
<subject>Brad Pitt
<predicate>nominateFor</predicate>
<object>Golden Globe Award</object>
</triplet>
- <triplet>
<subject>Brad Pitt
<predicate>hasbirthdate</predicate>
<object>December 18,1963</object>
</triplet>

the graph will be like this:
brad pitt will be represented in a rectangle and this rectangle will be connected with ellipses that represent the data as actor, producer ... etc., the relationship between the rectangle (brad pit) and actor for example be represented by a line with the word "is "
I hope that my idea is clear
baliram bhande 30-Apr-14 10:31am    
what you tried please paste your on the basis that we will help you.
Member 10743868 30-Apr-14 11:24am    
the idea here was to prepare an rdf file then get the graph from this file
I tried with the jena api
this is my code :






import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;














import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.ModelGraphInterface;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;



public abstract class Parcouretgraphe implements ModelGraphInterface {

private static final String Element = null;
static Model modele =ModelFactory.createDefaultModel();
StmtIterator iter = modele.listStatements();
static final Graph G=null; ;

public static void main(String args[]) {
try {

FileOutputStream sortie =new FileOutputStream("D:/eclipse_works/Hala/bin/result.xml");
File nomf = new File("C:/Users/Hela/Desktop/1_auto.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(nomf);
doc.getDocumentElement().normalize();

System.out.println("root of xml file" + doc.getDocumentElement().getNodeName());
NodeList nodes = doc.getElementsByTagName("triplet");
System.out.println("==========================");


for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String Resource = getValue("subject", element);

String Predicat = getValue("predicate", element);
String Objet =getValue("object", element);


CreateStatement(Resource , Predicat , Objet,sortie);
try {
FileOutputStream sortie1 =new FileOutputStream("D:/eclipse_works/Hala/bin/result.xml");


modele.write(new PrintWriter(System.out));
modele.write(sortie1);
}
catch(IOException e){
System.out.println("Exception caught"+e.getMessage());
}








/*Statement st =CreateModel(Resource , Predicat , Objet);
modele.add(st);

CreatGraph(CreateModel(Resource, Predicat, Objet));*/


}
} }catch (Exception ex) {
ex.printStackTrace();

}




}

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 CreateStatement(String resource, String predicat , String objet, FileOutputStream sortie){



Model modele =ModelFactory.createDefaultModel();

Resource R =modele.createResource(resource);
Property P = modele.createProperty(predicat);

modele.write(sortie);
modele.write(new PrintWriter(System.out));
/*return (modele.createStatement(R, P, objet));*/



}
/* public static void CreatGraph(Statement S){

Model modele =ModelFactory.createDefaultModel();
modele.add(S);
modele.getGraph();
}

*/

}
Sergey Alexandrovich Kryukov 30-Apr-14 14:18pm    
Please don't post such things in comments, use "Improve question" instead.
—SA

1 solution

I didn't read all that - but i'd use JGraph[^] for that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900