XML and java

I have some tags in my elements like

 <xsl:template match="jungle">
   <jungle>
        <xsl:copy-of select="date:formatDate(.)"/>
   </jungle>
</xsl:template>

 <xsl:template match="forest">
   <forest>
        <xsl:copy-of select="date:formatDate(.)"/>
   </forest>
</xsl:template>

I need to get the tag<xsl:template match="jungle"> and then add a few
elements in it like
 <xsl:template match="jungle">
   <xsl:variable name = "tree" />
   <jungle>
        <xsl:copy-of select="date:formatDate(.)"/>
   </jungle>
</xsl:template>

how do I get the  particular tag in a DOM xsl:template and match = jungle

 NodeList formats = root.getElementsByTagName("xsl:template");

do i need to loop thru each node or can i get a node by sepcifying
tagname and attribute?

Thanks
LVL 49
sunray_2003Asked:
Who is Participating?
 
lhankinsCommented:
Also - if you have the XML Doc already in a file, you can just do this :

      SAXReader xmlReader = new SAXReader();
      FileInputStream inStream = new FileInputStream("path-to-your-file");
      Document domDocument = xmlReader.read(inStream);
0
 
lhankinsCommented:
If you use Dom4j, its pretty easy :

   Element e = (Element)domDoc.selectSingleNode("//xsl:template[@match='jungle']");

That says : find the first <xsl:template> tag with an attribute of 'match' that has a value of 'jungle'.

BTW, to setup the domDocument in Dom4j, you do something like this :

      SAXReader xmlReader = new SAXReader();
      ByteArrayInputStream memStream = new ByteArrayInputStream(xmlString.getBytes());
      Document domDocument = xmlReader.read((InputStream)memStream);


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.