subbusaabu
subbusaabu

Reputation: 181

read xml file using java

The following is my xml file format

<?xml version="1.0" encoding="UTF-8"?>
<Hospitals>
<Hospital hospitalId="14">
    <HospitalName>aaa</HospitalName>
        <Department>
            <DepartmentName departmentId="21">card</DepartmentName> 
                <Clinics>
                    <ClinicName  clinicId="38">c7</ClinicName>
                    <Status Flag="0">0</Status>
                    <ClinicName  clinicId="39">c2</ClinicName>
                    <Status Flag="0">0</Status>

            </Clinics>
       </Department>
 </Hospital>
<Hospital hospitalId="15">
     <HospitalName>bbbb</HospitalName>
        <Department>
            <DepartmentName departmentId="22">dental</DepartmentName>
                <Clinics>
                    <ClinicName  clinicId="35">c6</ClinicName>
                    <Status Flag="0">0</Status>
                    <ClinicName  clinicId="36">c5</ClinicName>
                    <Status Flag="0">0</Status>
                                          </Clinics>
           </Department>
</Hospital>

help me with the java code to read from the xml to print the alues as shown below.I tried with this but I am able to print as the format shown below

Root element :Hospitals
----------------------
 hospital Id : 14
 Hospital Name : aaa
 department Id : 21
 Department Name : card
 clinicId : 38
 ClinicName : c7
 status : 0
 Flag : 0
 clinicId : 38
 ClinicName : c2
 status : 0
 Flag : 0
----------------------
hospital Id : 15
Hospital Name : bbbb    
department Id : 22
Department Name : dental
clinicId : 35
ClinicName : c6
status : 0
Flag : 0
clinicId : 38
ClinicName : c5
status : 0
Flag : 0

Any sort of help will help me to complete the work quickly...Thanks in advance

Upvotes: 0

Views: 303

Answers (3)

Michael Kay
Michael Kay

Reputation: 163322

I would suggest doing this with XSLT or XQuery; in both cases the code is far simpler than doing it in Java. Using Java is reasonable if you need to do some complex processing of the data within a Java application, but if you just want to extract some information and output it to a text file, it's much better to use higher-level tools.

Here's a starter for your XSLT stylesheet:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:variable name="NL" select="'&#xa;'"/>

<xsl:template match="Hospitals">
  <xsl:text>Root element: Hospitals</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="Hospital">
  <xsl:text>&#xa;------------------</xsl:text>
  <xsl:text>&#xa;hospital id: </xsl:text>
  <xsl:value-of select="@hospitalId"/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="HospitalName">
  <xsl:text>&#xa;hospital name: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="DepartmentName">
  <xsl:text>&#xa;department id: </xsl:text>
  <xsl:value-of select="@departmentId"/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:text>&#xa;department name: </xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

and more of the same.

You can of course run XSLT code from Java (or from the command line, or from Ant, etc). The JDK comes with an XSLT 1.0 processor built in, or you can download Saxon to get an XSLT 2.0 processor. This simple example only uses XSLT 1.0 but you will soon find yourself needing XSLT 2.0 features so you might as well start off that way.

Upvotes: 0

rogue lad
rogue lad

Reputation: 2452

Well , It is well known that XML parsing is done with the DOM and SAX but they are at the core. It's hard for a beginner to manage with the complex set of APIs. I would rather suggest to use a frame work , Apache Digester

It will ease , It is also using SAX but under the scene , you don't need to work with SAX.

Upvotes: 0

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

Which version of Java you are using?? if you have XSD defined for your XML there are many option by which you can parse your XML in java object and can read or do what ever operation you waant to do with the data.Here are few options for you

  1. JAXB 2.0
  2. XStream

There are other few from Apache and few others, if you are using JDK 6.0+ JaxB is being provided with the JDK while Xstream is very light and easy to use.

Upvotes: 1

Related Questions