以文本方式查看主题

-  W3CHINA.ORG讨论区 - 语义网·描述逻辑·本体·RDF·OWL  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  java中如何使用xpath解析带有默认名称空间的xml文档?  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=27075)


--  作者:一直在漂
--  发布时间:2/8/2006 3:37:00 PM

--  java中如何使用xpath解析带有默认名称空间的xml文档?
xml文档如下:
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg">  
    <g>
        <rect x="0" y="0" width="100" height="50" fill="none" stroke="black"/> 
        <rect x="0" y="100" width="100" height="50" fill="red" stroke="black"/> 
    </g>
</svg>
怎样利用xpath解析器找到“<rect x="0" y="100" width="100" height="50" fill="red" stroke="black"/>”节点呢?
我编写了一个java类,代码如下:
说明:XMLUtil是自己编写的一个常用类
public class testXPath{ 
 public static void main(String[] args) throws XPathFactoryConfigurationException {
  Element extendTest=null;  
  try {
   extendTest=XMLUtil.loadXMLDocumentFromFile("testXml.xml").getDocumentElement();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  }  
   //get an XPath processor  
        XPathFactory xpfactory = XPathFactory.newInstance();
        XPath xpathprocessor = xpfactory.newXPath();
        //create XPath expressions
        String nameString="red";         
        String xpath1 = "//rect[@fill='"+nameString+"']";       
        try{
         XPathExpression nodeXPath = xpathprocessor.compile(xpath1);
         
         NodeList tmpNodes = (NodeList)nodeXPath.evaluate(extendTest,
                 XPathConstants.NODESET);
      if(tmpNodes.item(0)!=null){
           System.out.println(XMLUtil.toString((Element)tmpNodes.item(0)));
      }
      else{
       System.out.println(tmpNodes.item(0));
      }
        }
        catch(XPathExpressionException e){
         e.printStackTrace();
        }   
  System.exit(0);
 }
}

如果没有xmlns时该类工作正常,可添加了xmlns属性后,默认名称空间作祟,找不到需要的节点了,高手请指点!



--  作者:一直在漂
--  发布时间:2/8/2006 4:47:00 PM

--  自问自答
被该问题困扰了几天后,终于找到了解决方法:哈哈哈哈
public class testXPath{ 
 public static void main(String[] args) throws XPathFactoryConfigurationException {
  Element extendTest=null;  
  try {
   extendTest=XMLUtil.loadXMLDocumentFromFile("testXml.xml").getDocumentElement();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  }  
   //get an XPath processor  
        XPathFactory xpfactory = XPathFactory.newInstance();
        XPath xpathprocessor = xpfactory.newXPath();
        xpathprocessor.setNamespaceContext(new MyNamespaceContext());       //create XPath expressions
        String nameString="red"; 
        //注意下面这句话,弄了几次都变不红,只有这样注释了        
        String xpath1 = "//xs:rect[@fill='"+nameString+"']";    
        try{
         XPathExpression nodeXPath = xpathprocessor.compile(xpath1);
         
         NodeList tmpNodes = (NodeList)nodeXPath.evaluate(extendTest,
                 XPathConstants.NODESET);
      if(tmpNodes.item(0)!=null){
           System.out.println(XMLUtil.toString((Element)tmpNodes.item(0)));
      }
      else{
       System.out.println(tmpNodes.item(0));
      }
        }
        catch(XPathExpressionException e){
         e.printStackTrace();
        }   
  System.exit(0);
 }
}

public class MyNamespaceContext implements NamespaceContext
{
    public String getNamespaceURI(String prefix)
    {        
     if (prefix.equals("xs"))
            return "http://www.w3.org/2000/svg";
        else
            return "";
    }
    
    public String getPrefix(String namespace)
    {
        
//      TODO Auto-generated method stub
     return "";
    }

 public Iterator getPrefixes(String arg0) {
  // TODO Auto-generated method stub
  return null;  
 }    
}  

运行后,输出结果就是“<rect fill="red" height="50" stroke="black" width="100" x="0" y="100" xmlns="http://www.w3.org/2000/svg"/>”



W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.875ms