[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Parse XML using Java Dom Parser

Posted on 2014-07-14
9
Medium Priority
?
168 Views
Last Modified: 2014-07-17
How do I parser the XML below using DOM Parser: I need to capture Nodes between <DATA RECORD>
<?xml version="1.0" ?>
<!DOCTYPE main [
  <!ELEMENT main (DATA_RECORD*)>
  <!ELEMENT DATA_RECORD (NEW_UPDATE?,GUILD_STUDIO_CODE?,STUDIO_NAME,CONTRACT_CODE?,CONTRACT_YEAR?,NEGOTIATED_PNH_RATE?,
  EFFECTIVE_DATE?,EXPIRATION_DATE?,SIGN_DATE?,AMPTP?,JPC?,AAAA?,ANA?,DATE_OF_UPDATE?)+>
  <!ELEMENT NEW_UPDATE (#PCDATA)>
  <!ELEMENT GUILD_STUDIO_CODE (#PCDATA)>
  <!ELEMENT STUDIO_NAME (#PCDATA)>
  <!ELEMENT CONTRACT_CODE (#PCDATA)>
  <!ELEMENT CONTRACT_YEAR (#PCDATA)>
  <!ELEMENT NEGOTIATED_PNH_RATE (#PCDATA)>
  <!ELEMENT EFFECTIVE_DATE (#PCDATA)>
  <!ELEMENT EXPIRATION_DATE (#PCDATA)>
  <!ELEMENT SIGN_DATE (#PCDATA)>
  <!ELEMENT AMPTP (#PCDATA)>
  <!ELEMENT JPC (#PCDATA)>
  <!ELEMENT AAAA (#PCDATA)>
  <!ELEMENT ANA (#PCDATA)>
  <!ELEMENT DATE_OF_UPDATE (#PCDATA)>
]>
<main>
  <DATA_RECORD>
    <NEW_UPDATE>NEW</NEW_UPDATE>
    <GUILD_STUDIO_CODE>007005852</GUILD_STUDIO_CODE>
    <STUDIO_NAME>New Concepts Ltd</STUDIO_NAME>
    <CONTRACT_CODE>I</CONTRACT_CODE>
    <CONTRACT_YEAR>1985</CONTRACT_YEAR>
    <NEGOTIATED_PNH_RATE></NEGOTIATED_PNH_RATE>
    <EFFECTIVE_DATE>01-JUL-1985</EFFECTIVE_DATE>
    <EXPIRATION_DATE>30-JUN-1987</EXPIRATION_DATE>
    <SIGN_DATE>1986/03/31 00:00:00</SIGN_DATE>
    <AMPTP>N</AMPTP>
    <JPC>N</JPC>
    <AAAA>N</AAAA>
    <ANA>N</ANA>
    <DATE_OF_UPDATE>16-NOV-1999</DATE_OF_UPDATE>
  </DATA_RECORD>
  <DATA_RECORD>
    <NEW_UPDATE>NEW</NEW_UPDATE>
    <GUILD_STUDIO_CODE>00700652</GUILD_STUDIO_CODE>
    <STUDIO_NAME>Old Concepts Ltd</STUDIO_NAME>
    <CONTRACT_CODE>CM</CONTRACT_CODE>
    <CONTRACT_YEAR>1993</CONTRACT_YEAR>
    <NEGOTIATED_PNH_RATE></NEGOTIATED_PNH_RATE>
    <EFFECTIVE_DATE>01-MAY-1993</EFFECTIVE_DATE>
    <EXPIRATION_DATE>30-APR-1996</EXPIRATION_DATE>
    <SIGN_DATE>1993/07/22 00:00:00</SIGN_DATE>
    <AMPTP>N</AMPTP>
    <JPC>N</JPC>
    <AAAA>N</AAAA>
    <ANA>N</ANA>
    <DATE_OF_UPDATE>27-JUL-1993</DATE_OF_UPDATE>
  </DATA_RECORD>
0
Comment
Question by:Vakils
  • 5
  • 4
9 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 40196390
I would tend to use XPath.

http://technojeeves.com/index.php/aliasjava1/26-xpath-tester

The pattern you'd want here is '//DATA_RECORD/*/text()'

 java XPathTester file:x.xml '//DATA_RECORD/*/text()'

Open in new window

0
 

Author Comment

by:Vakils
ID: 40198540
Hi,
Can you please explain why I need pattern? All I need is nodes and their values.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40198761
A pattern is to get nodes
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Vakils
ID: 40200603
Thanks. Does xpath have advantage over DOM parser?  It does get node value. I need node name too. Let's see if I can tweak code.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40200686
Does xpath have advantage over DOM parser?
Yes. It requires a lot less code. Of course, they are not mutually exclusive - XPath requires a DOM parser. The point is that getting nodes from that DOM without XPath requires more code
0
 

Author Comment

by:Vakils
ID: 40201001
I tweaked the code a bit and got foll. result which I wanted. XPath is really powerful for extracting xml data using expressions. Thanks for solution and introducing me to XPath. If you can optimize on the tweak below, pl do post.
Thanks!
      
                 if(nod.getNodeType() == Node.ELEMENT_NODE)
                {
                    System.out.print(nodeList.item(i).getNodeName() + " : "); 
                    if(nod.getFirstChild() != null)
                    {
                        System.out.println(nod.getFirstChild().getNodeValue());
                    }
                    else
                    {
                        System.out.println();
                    }
                }

Open in new window

The result:
run:
CONTRACT_CODE : 2
CONTRACT_YEAR :
CONTRACT_TITLE : 1061
CONTRACT_DESCRIPTION : L-U NON-BRDCST(KMBZ/KLTH/KCMO
EFFECTIVE_DATE : 31-JUL-1992
EXPIRATION_DATE : 30-APR-1996
EXTENSION_OF_END_DATE :
CONTRACT_CODE : 1
CONTRACT_YEAR :
CONTRACT_TITLE : 1062
CONTRACT_DESCRIPTION : L-U NATL COMML(KMBZ/KLTH/KCMO)
EFFECTIVE_DATE : 31-JUL-1992
EXPIRATION_DATE : 30-APR-1996
EXTENSION_OF_END_DATE :
BUILD SUCCESSFUL (total time: 30 seconds)
0
 

Author Closing Comment

by:Vakils
ID: 40201003
Thanks.
0
 

Author Comment

by:Vakils
ID: 40201011
Oh, and changed arg too, 2nd one   '//DATA_RECORD/*'
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40201558
Yes, that's OK if for some reason you want the Node instances instead of the content
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month18 days, 2 hours left to enroll

831 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