Ciao grazie per la risposta.Questa notte c'ho lavorato un pò su e ho buttato giù questo codice
// DOMRead.java
import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.w3c.dom.traversal.*;
// This program creates a DOM tree for the file specified as a command
// line option and demonstrates the use of a NodeFilter to obtain the
// Element nodes corresponding to objects in an XMI document.
public class DOMRead {
// Return the nodes that represent objects; the "visibility" attribute
// is optional, and XMI extension elements may have "xmi:id"
// attributes too, so this filter will not work for every XMI
// file, but it is good enough for the DOM example in this chapter.
private static class ObjectFilter implements NodeFilter {
public short acceptNode(Node n) {
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) n;
if (e.getAttributeNode("visibility") != null)
return NodeFilter.FILTER_ACCEPT;
}
return NodeFilter.FILTER_REJECT;
}
}
public static void main(String[] args) throws Exception {
DOMParser parser = new DOMParser();
parser.parse("file.xmi");
Document d = parser.getDocument();
DocumentTraversal dt = (DocumentTraversal) d;
//Create the NodeIterator with the filter created above. The
//NodeIterator will apply the filter before returning the next node.
NodeIterator it = dt.createNodeIterator(d.getDocumentElement(),NodeFilter.SHOW_ALL,new ObjectFilter(),true);
Node n = it.nextNode();
while (n != null) {
writeObject(n);
n = it.nextNode();
}
}
//Write the name of the Element node and the XML attributes and
//their values.
public static void writeObject(Node object) {
System.out.println(object.getNodeName().substring(4) + ":");
NamedNodeMap attribs = object.getAttributes();
for (int j = 0; j < attribs.getLength(); j++)
System.out.println(" " + attribs.item(j).getNodeName() +":" + attribs.item(j).getNodeValue());
}
}
Il problema è che dovrei stampare su un file solo classi e relativi attributi,mentre al momento stampo tutte le informazioni contenute in ogni nodo.Come posso effettuare una selezione delle sole informazioni che mi servono?Grazie(di seguito il file di output)
Class:
isAbstract:false
isActive:false
isLeaf:false
isRoot:false
isSpecification:false
name:Table
namespace:UMLModel.4
participant:UMLAssociationEnd.16 UMLAssociationEnd.19 UMLAssociationEnd.20 UMLAssociationEnd.22
visibility:public
xmi.id:UMLClass.7
Attribute:
changeability:changeable
isSpecification:false
name:Type
owner:UMLClass.7
ownerScope:instance
targetScope:instance
type:
visibility:public
xmi.id:UMLAttribute.8
Attribute:
changeability:changeable
isSpecification:false
name:Comp
owner:UMLClass.7
ownerScope:instance
targetScope:instance
type:
visibility:public
xmi.id:UMLAttribute.9
Attribute:
changeability:changeable
isSpecification:false
name:DT
owner:UMLClass.7
ownerScope:instance
targetScope:instance
type:
visibility:public
xmi.id:UMLAttribute.10
Class:
isAbstract:false
isActive:false
isLeaf:false
isRoot:false
isSpecification:false
name:Project
namespace:UMLModel.4
participant:UMLAssociationEnd.17 UMLAssociationEnd.23 UMLAssociationEnd.57 UMLAssociationEnd.60 UMLAssociationEnd.69 UMLAssociationEnd.72 UMLAssociationEnd.75
visibility:public
xmi.id:UMLClass.11
Attribute:
changeability:changeable
isSpecification:false
name:Name
owner:UMLClass.11
ownerScope:instance
targetScope:instance
type:
visibility:public
xmi.id:UMLAttribute.12
Attribute:
changeability:changeable
isSpecification:false
name:A
owner:UMLClass.11
ownerScope:instance
targetScope:instance
type:
visibility:public
xmi.id:UMLAttribute.13
Attribute:
changeability:changeable
isSpecification:false
name:Y
owner:UMLClass.11
ownerScope:instance
targetScope:instance
type:
visibility:public
xmi.id:UMLAttribute.14