Jeg har prøvet bare at kopiere din kode:
package XMLmodify;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class XMLmodify {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new FileReader("P:\\UML\\HHClassTest.xml")));
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if(prefix.equals("UML")) {
return "
http://org.omg/UML/1.3"; } else {
return null;
}
}
public String getPrefix(String namespace) {
if(namespace.equals("
http://org.omg/UML/1.3")) {
return "UML";
} else {
return null;
}
}
public Iterator getPrefixes(String namespace) {
return null;
}
});
NodeList clzes = (NodeList)xpath.evaluate("//XMI/XMI.content/UML:Model", doc.getDocumentElement(), XPathConstants.NODESET);
System.out.println(clzes.getLength());
for(int i = 0; i < clzes.getLength(); i++) {
Node clz = clzes.item(i);
System.out.println(clz.getAttributes().getNamedItem("visibility") + " " + clz.getAttributes().getNamedItem("name"));
}
System.out.println("done");
}
}
og det virker desværre heller ikke.
Dette er mit prøve .xml dokument (HHClassTest.xml):
<?xml version="1.0" encoding="UTF-8"?>
<XMI xmi.version="1.1" xmlns:UML="//org.omg/UML/1.3" timestamp="17. juni 2013 14:24:09">
<XMI.header>
<XMI.documentation>
<XMI.exporter>
</XMI.exporter>
<XMI.exporterVersion>
</XMI.exporterVersion>
</XMI.documentation>
<XMI.metamodel xmi.name="UML" xmi.version="1.4">
</XMI.metamodel>
</XMI.header>
<XMI.content>
<UML:Model name="rhapsody_test" visibility="public"
xmi.id="ML1">
</UML:Model>
</XMI.content>
</XMI>