0

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

4
  • use dom or sax api , should be easy Commented Jul 26, 2012 at 6:02
  • What exactly went wrong when you tried the tutorial you linked to? Commented Jul 26, 2012 at 6:04
  • @Tom I am not able to iterate through the Clinics Tag.It gives me only one clinic name data and status data, but for there are two for each hospital...and more over the printing format i shown is not getting for me. Commented Jul 26, 2012 at 6:40
  • @subbusaabu do you iterate over the ClinicName elements the way it's done with staff elements in the tutorial? Please post your code. Commented Jul 26, 2012 at 6:53

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank u I used JAXB technology And I got the required format And That too in a formatted way...
0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.