简体   繁体   English

使用Java读取xml文件

[英]read xml file using java

The following is my xml file format 以下是我的xml文件格式

<?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 帮助我用Java代码从xml读取以打印alues,如下所示。我尝试了此操作,但是我能够按照以下所示的格式进行打印

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 任何形式的帮助都将帮助我快速完成工作...在此先感谢

Which version of Java you are using?? 您正在使用哪个版本的Java? 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 如果您已经为XML定义了XSD,则可以通过多种选择来解析java对象中的XML,并且可以读取或执行您想对数据进行的任何操作。

  1. JAXB 2.0 JAXB 2.0
  2. XStream 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. 如果您使用的是JDK 6.0+,则来自Apache的其他产品和其他产品很少。Jstream随附了JaxB,而Xstream非常轻巧且易于使用。

Well , It is well known that XML parsing is done with the DOM and SAX but they are at the core. 好吧,众所周知,XML解析是使用DOM和SAX完成的,但它们是核心。 It's hard for a beginner to manage with the complex set of APIs. 对于初学者来说,很难使用复杂的API进行管理。 I would rather suggest to use a frame work , Apache Digester 我宁愿建议使用框架, Apache Digester

It will ease , It is also using SAX but under the scene , you don't need to work with SAX. 这会很容易,它也正在使用SAX,但是在现场,您不需要使用SAX。

I would suggest doing this with XSLT or XQuery; 我建议使用XSLT或XQuery进行此操作; in both cases the code is far simpler than doing it in Java. 在这两种情况下,代码都比在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. 如果您需要对Java应用程序中的数据进行一些复杂的处理,那么使用Java是合理的,但是如果您只想提取一些信息并将其输出到文本文件,那么使用高级工具会更好。

Here's a starter for your XSLT stylesheet: 这是您的XSLT样式表的入门版:

<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). 您当然可以从Java(或从命令行,或从Ant等)运行XSLT代码。 The JDK comes with an XSLT 1.0 processor built in, or you can download Saxon to get an XSLT 2.0 processor. JDK内置了XSLT 1.0处理器,或者您可以下载Saxon以获得XSLT 2.0处理器。 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. 这个简单的示例仅使用XSLT 1.0,但是您很快就会发现自己需要XSLT 2.0功能,因此不妨从此开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM