-
March 31st, 2014, 05:38 PM
#1
Xmi parsing
Hi at all,
I Should develop a java application that performs parsing xmi, and that print on a file classes with their attributes. I was able to parse and thought I'd put the names in a list of the classes and attributes in another. Unfortunately I do not know how to be able to print the classes and their attributes!
This is my code:
Code:
package mood.xmi;
import java.io.File;
import org.w3c.dom.*;
//classe che permette di creare un albero a partire da un file xml
import javax.xml.parsers.DocumentBuilderFactory;
//classe che permette di ottenere a partire da un xml un document che rappresenta il file xml
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.lang.String;
public class forum {
final static String ATTRIBUTES = "UML:Attribute";
final static String CLASSES = "UML:Class";
final static String FILE_ADDRESS = "FILE.xmi";
public static void main(String args[]) {
NodeList listClass;
NodeList listAttributes;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(FILE_ADDRESS));
doc.getDocumentElement().normalize();
System.out.println("==============================");
listClass = doc.getElementsByTagName(CLASSES);
int totalClass = listClass.getLength();
System.out.println("Total Class : " + totalClass);
for (int i = 0; i < listClass.getLength(); i++) {
Element link = (Element) listClass.item(i);
System.out.println("Class= "+ link.getAttribute("name"));
}
listAttributes = doc.getElementsByTagName(ATTRIBUTES);
int totalAttributes = listAttributes.getLength();
System.out.println("Attribute:"+totalAttributes);
NodeList linksAtributesHiding = doc.getElementsByTagName(ATTRIBUTES);
for (int i = 0; i < linksAtributesHiding.getLength(); i++) {
Element link = (Element) linksAtributesHiding.item(i);
System.out.println("Attribute= " + link.getAttribute("name"));
}
} catch (SAXParseException err) {
System.out.println("** Parsing error" + ", line "
+ err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
} catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
THANK U AT ALL
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|