• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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
0
sunray_2003
Asked:
sunray_2003
  • 2
1 Solution
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now