如何使用NameSpace uri解析XML文件

时间:2014-05-09 12:22:27

标签: java xpath namespaces

我无法从下面的xml文件中读取属性值。我是XPath的新手,并尝试了网上提供的所有解决方案,但没有运气。

下面是输入xml

<?xml version="1.0" encoding="UTF-8"?>
  <ns0:TPML xmlns:ns0="urn:x-schemas-xx:tpmbase/tpml/tpml_1_2">
<ns1:tpHeader xmlns:ns1="urn:x-schemas-xx:tpmbase/tpml/tpheader_1_2">
    <ns1:messageTimestamp>2010-02-22T11:49:24.13Z</ns1:messageTimestamp>
    <ns1:messageSender>Sml2Tpml</ns1:messageSender>
    <ns1:originatingSystem>
        <ns1:name>SummitGboUtp</ns1:name>
        <ns1:location>LDN</ns1:location>
    </ns1:originatingSystem>
    <ns1:trackingId>3345069L</ns1:trackingId>
    <ns1:tradeId>3345069L</ns1:tradeId>
    <ns1:transactionId/>
    <ns1:versionNo>1</ns1:versionNo>
 </ns1:tpHeader>
</ns0:TPML>

这是java代码

package com.db.test.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class SummitTestXSL 
{
    public static void main(String arg[])
    {

        try 
        {
            FileInputStream file = new FileInputStream(new File("TestXml.xml"));
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder =builderFactory.newDocumentBuilder();
            Document xmlDocument = builder.parse(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            xPath.setNamespaceContext(new MyNamespaceContext());

            System.out.println("*************************");

            String expression="/ns0:TPML/ns1:tpHeader/ns1:messageTimestamp";

            System.out.println(expression);

            String str = xPath.compile(expression).evaluate(xmlDocument);

            System.out.println(str);

        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {

        }

    }


    private static class MyNamespaceContext implements NamespaceContext {

        public String getNamespaceURI(String prefix) {
            if("ns0".equals(prefix)) {
                return "urn:x-schemas-xx:tpmbase/tpml/tpml_1_2";
            }
            else if("ns1".equals(prefix))
            {
                return "urn:x-schemas-xx:tpmbase/tpml/tpheader_1_2";
            }
            return null;
        }

        public String getPrefix(String namespaceURI) {
            return null;
        }

        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

    }
}

我想读取messageTimeStamp的值。

1 个答案:

答案 0 :(得分:1)

您似乎在NamespaceAware上设置了DocumentBuilderFactory。您需要在创建builder

之前执行此操作
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);     //  <---- This line
DocumentBuilder builder =builderFactory.newDocumentBuilder();