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

XML parsing STEP BY STEP JAVA

Hi! i have this xml  file & need to parse so it look like  table with heading field
please give me a step by step solution using java  as soon as poss.500 points

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=" http://www.developer.cognos.com/schemas/bmt/32/3 " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://www.developer.cognos.com/schemas/bmt/32/3 BMTMod.xsd ">
<name>something</name>
<locales>
<locale>en</locale>
</locales>
<defaultLocale>en</defaultLocale>
<namespace>
<name locale="en">DOTNET</name>
<lastChanged>2006-06-14T08:49:20</lastChanged>
<namespace><name locale="en">some</name></namespace>
<namespace><name locale="en">thing</name></namespace>
<namespace><name locale="en">Presentation</name>
<namespace>
<name>Package1</name>
<shortcut><name>Object11</name></shortcut>
<folder><name>Folder11</name><shortcut><name>Object111</name> </shortcut></folder>
<folder>
<name>Folder12</name>
<shortcut><name>Object121</name></shortcut>
<shortcut><name>Object122</name></shortcut>
<shortcut><name>Object123</name></shortcut>
</folder>
</namespace>
<namespace>
<name>Package2</name>
<folder>
<name>Folder21</name>
<shortcut><name>Object21</name> </shortcut>
<folder><name>Folder211</name><shortcut><name>Object2111</name> </shortcut></folder>
</folder>
<folder>
<name>Folder22</name>
<shortcut><name>Object221</name> </shortcut>
<shortcut><name>Object222 </name> </shortcut>
<folder>
<name>Folder221</name>
<shortcut><name>Object2211</name> </shortcut>
<folder>
<name>FOlder2211</name>
<shortcut><name>Object22111</name> </shortcut>
</folder>
</folder>
</folder>
</namespace>
</namespace>
</namespace>
</project>
 
And I want the output like this:
Package Name
Object Name
Path
Package1
Object11
      

Package1/Object11

Package1
      

Object111
      

Package1/Folder11/Object111

Package1
      

Object121
      

Package1/Folder12/Object121

Package1
      

Object122
      

Package1/Folder12/Object122

Package1
      

Object123
      

Package1/Folder12/Object123

Package2
      

Object21
      

Package2/Folder21/Object21

Package2
      

Object2111
      

Package2/Folder21/Folder211/Object2111

Package2
      

Object221
      

Package2/Folder22/Object221

Package2
      

Object222
      

Package2/Folder22/Object222

Package2
      

Object2211
      

Package2/Folder22/Folder221/Object2211

Package2
      

Object22111
      

Package2/Folder22/Folder221/Folder2211/Object22111

 
      

 
      

 

 
      

 
      

 

 
      

 
      

 

Please give me some solution using XSLT or any other way

Thanks in advance
0
akhulna
Asked:
akhulna
  • 4
  • 2
1 Solution
 
YagantappaCommented:
You need have xsl file for the corresponding xml file. Read both xml and xsl file into string variables and call mergeResponse method as show below. Below is the complete code transform xml and xsl into html.
You need xalan.jar and jdk1.4.
Below is the sample code.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.xalan.processor.TransformerFactoryImpl;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class test
{

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        String xmlFile = ""; // xml file content
        String xslFile = ""; // xsl file content
       
        System.out.println(mergeResponse(xmlFile, xslFile));

    }

    public static Document getNewDocument(String str) throws ParserConfigurationException, IOException, SAXException,
            SAXParseException
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();
        // Document document = db.parse(str);
        Document document = db.parse(new InputSource(new StringReader(str)));
        // Document document = getDocument(db, str);
        return document;
    }

    public static Document getNewDocument() throws ParserConfigurationException, IOException, SAXException, SAXParseException
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.newDocument();
        return document;
    }

    /**
     *
     * @param xslPath
     * @param doc
     * @param out
     * @throws TransformerException
     * @throws TransformerConfigurationException
     * @throws ClassNotFoundException
     * @throws FileNotFoundException
     */
    public static String process(String xsl, Document doc) throws TransformerException, TransformerConfigurationException,
            ClassNotFoundException, FileNotFoundException
    {
        StringWriter sos = new StringWriter();

        StringReader fin = new StringReader(xsl);
        /*
         * try{ System.out.println("URL in Credit check: "); java.net.URL url = new
         * URL(HorizonLOSProperties.getString("HTTP_SERVER")+"/XMLEntities.res");
         * org.apache.xalan.serialize.CharInfo.XML_ENTITIES_RESOURCE = url.toString();
         * System.out.println("URL XML Entities: "+url.toString()); }catch(Exception
         * e){e.printStackTrace();}
         */
        TransformerFactory tFactory = TransformerFactoryImpl.newInstance();
        Transformer transformer = tFactory.newTransformer(new StreamSource(fin));
        DOMSource domSource = new DOMSource(doc);
        StreamResult streamResult = new StreamResult(sos);

        try
        {
            transformer.transform(domSource, streamResult);
        }
        catch (TransformerException e)
        {
            e.printStackTrace();
        }
        return sos.toString();
    }

    /**
     *
     * @param xslPath
     * @param doc
     * @param file
     * @throws TransformerException
     * @throws TransformerConfigurationException
     * @throws ClassNotFoundException
     * @throws FileNotFoundException
     */
    public static void process(String xslPath, Document doc, File file) throws TransformerException,
            TransformerConfigurationException, ClassNotFoundException, FileNotFoundException
    {
        FileInputStream fin = new FileInputStream(xslPath);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new StreamSource(fin));
        DOMSource domSource = new DOMSource(doc);
        StreamResult streamResult = new StreamResult(file);
        try
        {
            transformer.transform(domSource, streamResult);
        }
        catch (TransformerException e)
        {
            e.printStackTrace();
        }
    }

    public static Document getXMLDocumentFile(String path) throws Exception
    {
        Document doc = null;
        try
        {
            doc = getNewDocument(path);
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (SAXParseException se)
        {
            throw new Exception(se);
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        return doc;
    }

    public static String applyXSL(Document xml, String xsl) throws ParserConfigurationException, IOException, SAXException,
            SAXParseException
    {
        String text = null;
        try
        {
            text = process(xsl, xml);
        }
        catch (TransformerConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (TransformerException e)
        {
            e.printStackTrace();
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        return text;
    }

    public static String readFromFile(String inputPath) throws IOException
    {
        String str = "";
        char c = ' ';
        int i;
        FileInputStream fin = null;
        try
        {
            fin = new FileInputStream(inputPath);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        do
        {
            i = fin.read();
            if (i != -1)
            {
                c = (char) i;
                str += c;
            }

        } while (i != -1);
        fin.close();
        return str;
    }

    public static String mergeResponse(String xml, String xslDoc) throws Exception
    {
        String response = "";
        try
        {
            response = applyXSL(getXMLDocumentFile(xml), xslDoc);
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (SAXParseException se)
        {
            throw new Exception(se);
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        return response;
    }
}
0
 
akhulnaAuthor Commented:
could u help me with the xsl for this.please, i've trying.....since nested nodes.......knna having hard time
0
 
YagantappaCommented:
Here you go. It might require some changes.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:variable name="var1" select="/project/namespace/namespace/namespace/name" />
   <xsl:variable name="var2" select="/project/namespace/namespace/namespace/shortcut/name" />
   <xsl:variable name="var3" select="/project/namespace/namespace/namespace/folder/shortcut/name" />
   <xsl:variable name="var4" select="/project/namespace/namespace/namespace/folder/name" />

   <xsl:template match="/">
      <html>
         <body class="BodyClass" bgcolor="White">
            <br>
            </br>

            <table width="100%" border="0">
               <tr width="100%" align="Center">
                  <th width="5%">Package Name</th>

                  <th width="10%">Object Name</th>

                  <th width="10%">Path</th>
               </tr>

               <xsl:for-each select="/project/namespace/namespace/namespace">
                  <tr width="100%" align="Center">
                     <td width="10%">
                        <xsl:value-of select="name">
                        </xsl:value-of>
                     </td>

                     <td width="10%">
                        <xsl:value-of select="shortcut/name">
                        </xsl:value-of>
                     </td>

                     <td width="10%">
                        <xsl:value-of select="$var1">
                        </xsl:value-of>/
                        <xsl:value-of select="$var2">
                        </xsl:value-of>
                     </td>

                     <xsl:for-each select="/project/namespace/namespace/namespace/folder/shortcut">
                        <tr width="100%" align="Center">
                           <td width="10%">
                              <xsl:value-of select="../../name">
                              </xsl:value-of>
                           </td>

                           <td width="10%">
                              <xsl:value-of select="name">
                              </xsl:value-of>
                           </td>
                           <td width="10%">
                              <xsl:value-of select="../../name">
                              </xsl:value-of>/
                              <xsl:value-of select="../name">
                              </xsl:value-of>/
                            <xsl:value-of select="name">
                            </xsl:value-of>
                           </td>
                          
                        </tr>
                     </xsl:for-each>
                     <xsl:for-each select="/project/namespace/namespace/namespace/folder/folder">
                        <tr width="100%" align="Center">
                           <td width="10%">
                              <xsl:value-of select="../../name">
                              </xsl:value-of>
                           </td>

                           <td width="10%">
                              <xsl:value-of select="shortcut/name">
                              </xsl:value-of>
                           </td>
                           <td width="10%">
                              <xsl:value-of select="../../name">
                              </xsl:value-of>/
                              <xsl:value-of select="folder/name">
                              </xsl:value-of>/
                            <xsl:value-of select="shortcut/name">
                            </xsl:value-of>
                           </td>
                          
                        </tr>
                     </xsl:for-each>                                          
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
YagantappaCommented:
Here is the better one

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:variable name="var1" select="/project/namespace/namespace/namespace/name" />

   <xsl:variable name="var2" select="/project/namespace/namespace/namespace/shortcut/name" />

   <xsl:variable name="var3" select="/project/namespace/namespace/namespace/folder/shortcut/name" />

   <xsl:variable name="var4" select="/project/namespace/namespace/namespace/folder/name" />

   <xsl:template match="/">
      <html>
         <body class="BodyClass" bgcolor="White">
            <br>
            </br>

            <table width="100%" border="0">
               <tr width="100%" align="Center">
                  <th width="5%">Package Name</th>

                  <th width="10%">Object Name</th>

                  <th width="10%">Path</th>
               </tr>

               <xsl:for-each select="/project/namespace/namespace/namespace">
                  <tr width="100%" align="Center">
                     <td width="10%">
                        <xsl:value-of select="name">
                        </xsl:value-of>
                     </td>

                     <td width="10%">
                        <xsl:value-of select="shortcut/name">
                        </xsl:value-of>
                     </td>

                     <td width="10%">
                     <xsl:value-of select="name">
                     </xsl:value-of>

                     /
                     <xsl:value-of select="shortcut/name">
                     </xsl:value-of>
                     </td>

                     <xsl:for-each select="folder/shortcut">
                        <tr width="100%" align="Center">
                           <td width="10%">
                              <xsl:value-of select="../../name">
                              </xsl:value-of>
                           </td>

                           <td width="10%">
                              <xsl:value-of select="name">
                              </xsl:value-of>
                           </td>

                           <td width="10%">
                           <xsl:value-of select="../../name">
                           </xsl:value-of>

                           /
                           <xsl:value-of select="../name">
                           </xsl:value-of>

                           /
                           <xsl:value-of select="name">
                           </xsl:value-of>
                           </td>

                           <xsl:for-each select="../folder">
                              <tr width="100%" align="Center">
                                 <td width="10%">
                                    <xsl:value-of select="../../name">
                                    </xsl:value-of>
                                 </td>

                                 <td width="10%">
                                    <xsl:value-of select="shortcut/name">
                                    </xsl:value-of>
                                 </td>

                                 <td width="10%">
                                 <xsl:value-of select="../../name">
                                 </xsl:value-of>

                                 /
                                 <xsl:value-of select="name">
                                 </xsl:value-of>

                                 /
                                 <xsl:value-of select="shortcut/name">
                                 </xsl:value-of>
                                 </td>

                                 <xsl:for-each select="../folder/folder">
                                    <tr width="100%" align="Center">
                                       <td width="10%">
                                          <xsl:value-of select="../../../name">
                                          </xsl:value-of>
                                       </td>

                                       <td width="10%">
                                          <xsl:value-of select="shortcut/name">
                                          </xsl:value-of>
                                       </td>

                                       <td width="10%">
                                       <xsl:value-of select="../../name">
                                       </xsl:value-of>

                                       /
                                       <xsl:value-of select="name">
                                       </xsl:value-of>

                                       /
                                       <xsl:value-of select="shortcut/name">
                                       </xsl:value-of>
                                       </td>
                                    </tr>
                                 </xsl:for-each>
                              </tr>
                           </xsl:for-each>
                        </tr>
                     </xsl:for-each>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

0
 
YagantappaCommented:
Did it work....?
0
 
akhulnaAuthor Commented:
yes, thank u very much, i can work out the formating here, thanks again
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.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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