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");

しかし、ノードリストでnullになっています。

これは私のコードです:

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;

私はattributeElementsのnullを取得しています

JSP ページにパス、デコレータ、および除外の値を表示したいのですが、xpath 式でノードのリストを取得できません。

mappingXpathでノード要素を読み取るためのソリューションが必要です。

4

2 に答える 2

3

[編集] /sitemesh/mapping も機能します。

ここでの問題は、 nodeList が XPathConstants.NODESET にマップされている間に XPathConstants.NODE のエクスプレスを評価することです。以下のリンクを参照してください。

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 に答える