Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9214251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:56:07+00:00 2026-06-18T01:56:07+00:00

this is my XML file : <sitemesh> <mapping path=/editor/tempPage/** exclude=true/> <mapping decorator=/WEB-INF/views/decorators/detailstheme.jsp path=/* exclude=false

  • 0

this is my XML file :

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

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

    </sitemesh>

I want list of mapping node with their attribute values.
this should be done using Xpath.

my xpath expression is :

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

but i am getting null in nodelist.

this is my code:

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;

i am getting null for attributeElements

i want to show values of path,decorator and exclude on JSP page.But i am unable to get list of node through xpath expression.

I want solution for reading mapping node element in Xpath.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:56:08+00:00Added an answer on June 18, 2026 at 1:56 am

    [edit] /sitemesh/mapping also works .

    The issue here is that you evaluating the express for XPathConstants.NODE while the nodeList maps to XPathConstants.NODESET. please refer below link.

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

    Added sample code for illustration purpose only:

    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;
    }
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this xml file. <movies> <movie> <score>8.582207</score> <popularity>3</popularity> <translated>true</translated> <adult>false</adult> <language>en</language> <original_name>Transformers</original_name> <name>Transformers</name>
trying to scrape this xml file on the web with urllib and cElementTree. I
I have sitemesh with spring working, this is the configuration: decorator.xml <?xml version=1.0 encoding=UTF-8?>
I am having a xml file.this having the path for my log file(txt).This xml
With this XML file - <Instructions> <Admin> <Instruction> Steps must be completed in order.&lt;/br&gt;
This is my Xml file.i want to transform this xml file into another customized
I have this xml file which has text init. i.e Hi my name is
I got this XML file: <Msg UserText=start 0> </Msg> <Msg UserText=A> </Msg> <Msg UserText=A>
I have this xml file <?xml version=1.0 encoding=UTF-8?> <bo:C837ClaimParent xsi:type=bo:C837ClaimParent xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:bo=http://somelongpathHere/process/bo> <claimAux> ...
I am receiving this XML file which I would like to validate. Ideally I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.