如何获取包含冒号的xml属性值?

时间:2014-02-19 09:37:10

标签: java xml xpath

我试图在java中使用Xpath解析xml文件。我需要在文本元素下获取属性值为xml:lang =" en"的所有元素值。

这是我的xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<image id="10001" file="images/2/10001.png">
   <name>Lake two mountains.png</name>
   <text xml:lang="en">
      <description />
      <comment />
      <caption article="text/en/4/335157">Location map of Lake of Two Mountains.  </caption>
   </text>
   <text xml:lang="de">
      <description/>
      <comment />
      <caption article="text/de/5/441485">Lage des Lac des Deux Montagnes (ganz rechts liegt Montréal)</caption>
   </text>
   <text xml:lang="fr">
      <description />
      <comment />
      <caption />
   </text>
   <comment>({{Information |Description= Location map of Lake of Two Mountains in Quebec, Canada. |Source= based on Image:Oka map with roads.png. |Date= |Author= P199 |Permission= |other_versions= }})</comment>
   <license>GFDL</license>
</image>

这是我的java代码片段:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document xmlDocument = null;
try {
       builder = builderFactory.newDocumentBuilder();
    } 
catch (ParserConfigurationException e) {
  e.printStackTrace();  
}    

try {
       xmlDocument = builder.parse(new FileInputStream(fileEntry.getAbsolutePath()));
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            XPath xPath =  XPathFactory.newInstance().newXPath();

            //prepare node expressions
            String nameExpr = "/image/name";
            String descriptionExpr = "/image/text[@lang='en']/description";
            String captionExpr = "/image/text[@lang='en']/caption";
            String commentExpr = "/image/text[@lang='en']/comment";

            //read a string value
            String name = xPath.compile(nameExpr).evaluate(xmlDocument);
            String description = xPath.compile(descriptionExpr).evaluate(xmlDocument);
            String caption = xPath.compile(captionExpr).evaluate(xmlDocument);
            String comment = xPath.compile(commentExpr).evaluate(xmlDocument);

我尝试了一些Xpath表达式来获取元素值,例如:

(1)/ image / text [@xml:lang =&#39; en&#39;] / description&#34;哪个不起作用。

(2)/ image / text [@lang =&#39; en&#39;] / description&#34;工作正常。

我很想知道第一个Xpath表达式有什么问题。

先谢谢。

1 个答案:

答案 0 :(得分:2)

对于某些(可能是历史的)原因,DocumentBuilderFactory默认情况下名称空间感知。在调用setNamespaceAware(true)之前,必须在工厂中调用newDocumentBuilder(),因为XPath仅适用于已被解析为名称空间感知的XML。

然后我建议使用lang function进行实际测试:

/image/text[lang('en')]/description