Solved

XML parsing STEP BY STEP JAVA

Posted on 2006-06-20
6
359 Views
Last Modified: 2013-11-23
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
Comment
Question by:akhulna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 4

Expert Comment

by:Yagantappa
ID: 16957548
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
 

Author Comment

by:akhulna
ID: 16960222
could u help me with the xsl for this.please, i've trying.....since nested nodes.......knna having hard time
0
 
LVL 4

Expert Comment

by:Yagantappa
ID: 16967328
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Accepted Solution

by:
Yagantappa earned 500 total points
ID: 16967484
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
 
LVL 4

Expert Comment

by:Yagantappa
ID: 16982830
Did it work....?
0
 

Author Comment

by:akhulna
ID: 16983377
yes, thank u very much, i can work out the formating here, thanks again
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Simple Linear Regression
Six Sigma Control Plans

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question