Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zybnet;
- 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.XPathFactory;
- import org.w3c.dom.NodeList;
- public class ColladaXPath {
- public static void main(String[] args) throws Exception {
- NodeList coords = (NodeList) xpath.evaluate("//c:triangles/c:p/text()",
- builder.parse(new URL("http://purple-works.com/yardstick/tablemaya.dae").openStream()),
- XPathConstants.NODESET);
- for (int i = 0; i < coords.getLength(); i++) {
- System.out.println(String.format("This <p> contains %d indices:" ,
- coords.item(i).getNodeValue().split("\\s+").length));
- }
- }
- static XPath xpath;
- static DocumentBuilder builder;
- static {
- DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
- domFactory.setNamespaceAware(true); // !IMPORTANT
- try {
- builder = domFactory.newDocumentBuilder();
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- }
- xpath = XPathFactory.newInstance().newXPath();
- xpath.setNamespaceContext(new ColladaNamespaceCtx());
- }
- private static class ColladaNamespaceCtx implements NamespaceContext {
- @Override
- public String getNamespaceURI(String prefix) {
- return "http://www.collada.org/2005/11/COLLADASchema";
- }
- @Override
- public String getPrefix(String namespaceURI) {
- return "c";
- }
- @Override
- public Iterator<?> getPrefixes(String namespaceURI) {
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement