1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
public class NutStyle {
public NutStyle() throws XPathExpressionException, IOException, JDOMException {
//création de la source
InputSource source = new InputSource(getClass().getResourceAsStream("./nuts.xml"));
//création du XPath
XPathFactory fabrique = XPathFactory.newInstance();
XPath xpath = fabrique.newXPath();
NamespaceContext namespace = new NamespaceContext(){
public String getNamespaceURI(String prefix){
if("Filter".equals(prefix)){
return "http://www.opengis.net/ogc";
}
else if("BBOX".equals(prefix)) {
return "http://www.opengis.net/ogc";
}
else if("Box".equals(prefix)) {
return "http://www.opengis.net/gml";
}
else if("coordinates".equals(prefix)) {
return "http://www.opengis.net/gml";
}
else{
return "http://www.opengis.net/sld";
}
}
public String getPrefix(String namespaceURI){
return null;
}
public Iterator getPrefixes(String namespaceURI){
return null;
}
};
xpath.setNamespaceContext(namespace);
XPathExpression exp = xpath.compile("StyledLayerDescriptor:*/NamedLayer:*/UserStyle:*/FeatureTypeStyle:*/Rule:*/Filter:*/BBOX:*/Box:*/coordinates:*");
Node node = (Node)exp.evaluate(source,XPathConstants.NODE);
System.out.println("node : "+node.getTextContent());
node.setTextContent("2,3,4,5");
System.out.println("node : "+node.getTextContent());
}
public static void main(String[] args){
try {
new NutStyle();
} catch (Exception e) {
e.printStackTrace();
}
}
} |