1

这是我的 XML 文件:

<sitemesh>
    <mapping path="/editor/tempPage/**" exclude="true"/>

        <mapping decorator="/WEB-INF/views/decorators/detailstheme.jsp"
            path="/*" exclude="false" />

    </sitemesh>

我想要映射节点列表及其属性值。这应该使用 Xpath 来完成。

我的 xpath 表达式是:

expr = xpath.compile("/sitemesh/mapping");

但我在节点列表中得到空值。

这是我的代码:

Map<String,String> map=new HashMap<String, String>();

        // reading xml file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document doc = null;
        XPathExpression expr = null;
        try {
            builder = factory.newDocumentBuilder();
            // creating input stream
            doc = builder.parse(file);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();
            expr = xpath.compile("//mapping");
        } catch (Exception e) {
            LOG.error("some exception message", e);
        }


        //------------------------------------------------------------------------

        NodeList attributeElements = null;
        try {
            attributeElements =(NodeList)expr.evaluate(doc, XPathConstants.NODE);

        } catch (XPathExpressionException e) {
            LOG.error("some exception message", e);
        }
        System.out.println("lenght:"+attributeElements.getLength());
        for (int i = 0; i < attributeElements.getLength(); i++) {
                Node node=attributeElements.item(i);
                System.out.println("node:"+node.getNodeValue());
             NamedNodeMap attrs = node.getAttributes();  
              for(i = 0 ; i<attrs.getLength() ; i++) {
                Attr attribute = (Attr)attrs.item(i);  
                System.out.println("Node Attributes : " + attribute.getName()+" = "+attribute.getValue());
              }
        }


        //-------------------------------------------------------------------------

        // writing xml file
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer;
        try {
            transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(file);// creating output
                                                            // stream
            transformer.transform(source, result);
        } catch (Exception e) {
            LOG.error("some exception message", e);
        }

        return   map;

我的属性元素为空

我想在 JSP 页面上显示路径、装饰器和排除的值。但我无法通过 xpath 表达式获取节点列表。

mapping我想要在 Xpath中读取节点元素的解决方案。

4

2 回答 2

3

[编辑] /sitemesh/mapping 也可以。

这里的问题是您评估XPathConstants.NODE的 express而 nodeList 映射到XPathConstants.NODESET。请参考以下链接。

http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPathConstants.html#NODESET

添加示例代码仅用于说明目的:

public void testXpathExpr(){

String testXML = "<sitemesh><mapping path=\"/editor/tempPage/**\" exclude=\"true\"/><mapping decorator=\"/WEB-INF/views/decorators/detailstheme.jsp\" path=\"/*\" exclude=\"false\" /></sitemesh>";

NodeList nodeList = getNodeList(testXML);
}

private NodeList getNodeList(String xml) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = docFactory.newDocumentBuilder();

  document = builder.parse(new ByteArrayInputStream( xml.getBytes() ) );
  XPathExpression exprPath = xpath.compile(xpathExpr);
  NodeList nodeList = (NodeList) exprPath.evaluate(document, XPathConstants.NODESET);;
return nodeList;
}

希望这可以帮助!

于 2013-01-31T07:50:54.473 回答
1

你的 xpath 非常适合我。下面是示例代码:

public class Parser {

    public static void main(String[] args) throws Exception, Exception {
        final DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        final DocumentBuilder builder = factory.newDocumentBuilder();
        final Document doc = builder.parse("src/sitemesh.xml");
        final XPathFactory xPathfactory = XPathFactory.newInstance();
        final XPath xpath = xPathfactory.newXPath();
        final XPathExpression expr = xpath.compile("/sitemesh/mapping");
        Object node = expr.evaluate(doc, XPathConstants.NODE);

        System.out.println(node);
    }
}

sitemesh.xml 包含您的示例输入。

于 2013-01-31T07:58:03.550 回答