Parse XML using Java Dom Parser

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>
VakilsDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VakilsDeveloperAuthor Commented:
Hi,
Can you please explain why I need pattern? All I need is nodes and their values.
0
CEHJCommented:
A pattern is to get nodes
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

VakilsDeveloperAuthor Commented:
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
CEHJCommented:
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
VakilsDeveloperAuthor Commented:
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
VakilsDeveloperAuthor Commented:
Thanks.
0
VakilsDeveloperAuthor Commented:
Oh, and changed arg too, 2nd one   '//DATA_RECORD/*'
0
CEHJCommented:
Yes, that's OK if for some reason you want the Node instances instead of the content
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.