Java read xml file

Discussion in 'General Computer Support' started by java_newbie, May 26, 2009.

  1. java_newbie

    java_newbie

    Joined:
    May 25, 2009
    Messages:
    1
    Likes Received:
    0
    Hello all,

    I'm trying to read the contents of an XML file from my Java program.

    ==================================================
    logDay0.xml
    ==================================================

    <?xml version="1.0" ?>
    <root gmtdate="05-22-2009" gmttime="03:45:52" totalEvents="479">
    <event id="1" gmttimecode="1242884080" gmtdate="05-21-2009" gmttime="05:34:40" type="Added" category="DPI" msg="Heartbeat Lost" />
    <event id="2" gmttimecode="1242884130" gmtdate="05-21-2009" gmttime="05:35:30" type="Received" category="DPI" msg="splice_insert for source 0x0, event 000061D4, program 0, avail 0 of 0" />
    <event id="17" gmttimecode="1242885494" gmtdate="05-21-2009" gmttime="05:58:14" type="Received" category="DPI" msg="SCTE104 VANC message embedded for source 0x0, event 0x000061D5" />
    <event id="4" gmttimecode="1242884130" gmtdate="05-21-2009" gmttime="05:35:30" type="Received" category="DPI" msg="Relay #1 triggered by SCTE35 splice_insert for Source 0x00, Event 00025044" />
    <event id="7" gmttimecode="1242884130" gmtdate="05-21-2009" gmttime="05:35:30" type="Received" category="DPI" msg="GP output #2 triggered by SCTE35 splice_insert for Source 0x00, Event 00025044" />
    <event id="8" gmttimecode="1242884130" gmtdate="05-21-2009" gmttime="05:35:30" type="Received" category="DPI" msg="SCTE104 VANC message embedded for source 0x0, event 0x000061D4" />
    </root>
    ================================================== ==

    I want to extract info from the tag and after some parsing, display
    them as:

    event id=17 gmttimecode=1242885494 gmtdate=05-21-2009 gmttime=05:58:14 type=Received category=DPI msg=SCTE104 VANC message embedded for source 0x0, event 0x000061D5
    event id=8 gmttimecode=1242884130 gmtdate=05-21-2009 gmttime=05:35:30 type=Received category=DPI msg=SCTE104 VANC message embedded for source 0x0, event 0x000061D4

    I don't want anything fancy, just some ability to specify a file and extract specific tags that contain msg: SCTE104 xxxx and print out the results and ignore the tags with no SCTE104. A sample java program would be helpful.


    Thanks
     
    java_newbie, May 26, 2009
    #1
    1. Advertisements

  2. java_newbie

    torontolife

    Joined:
    Jun 4, 2009
    Messages:
    32
    Likes Received:
    0
    Don't reinvent the wheel, use a library instead!

    dom4j and JDOM are two nice XML libraries for Java, and you can parse XML as simply as calling a function.

    See dom4j.org and jdom.org (sorry, I can't post links due to this pesky forum thing)
     
    torontolife, Jun 4, 2009
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.