XML Parser

Hello,

I have made an xml parser and now I want to parse only some specific elements from xml file according to another txt file (datasource),

i.e. there are some common elements in both files (xml, txt) or some elements that are in the xml and aren't in the txt, etc.

I would like some help about how I'm gonna do it (compare the two files and send results to the output).

PS: The xml file looks like this:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

name="EXA_INFOTEXT22"

columnCount="1"

printOrder="Vertical"

orientation="Portrait"

pageWidth="595"

pageHeight="842"

columnWidth="559"

columnSpacing="0"

leftMargin="18"

rightMargin="18"

topMargin="14"

bottomMargin="0"

whenNoDataType="NoPages"

isTitleNewPage="false"

isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="0" />

<import value="java.util.*" />

<import value="net.sf.jasperreports.engine.*" />

<import value="net.sf.jasperreports.engine.data.*" />

<parameter name="IMG_ROOT" isForPrompting="false" class="java.lang.String">

<defaultValueExpression ><![CDATA["H:/JReports/"]]></defaultValueExpression>

</parameter>

<field name="evidSrv" class="java.lang.String"/>

<group name="evidSrv" isStartNewColumn="false" isStartNewPage="false" isResetPageNumber="false" isReprintHeaderOnEachPage="false" minHeightToStartNewPage="0" >

<groupExpression><![CDATA[$F{evidSrv}]]></groupExpression

.........................................................................................................

and the datasource file looks like this:

Procedure: web.ifc_print.print_car_loan_package

Columns:

pIdEmp [NUMBER]

pPackageCode [VARCHAR2]

pPackageNumCredit [VARCHAR2]

pIdCreditArr [VARCHAR2]

pCursor [REF CURSOR]

JasperReports XML document fields:

><field name="preview" class="java.lang.String"/>

<field name="packageCode" class="java.lang.String"/>

<field name="dateCreated" class="java.util.Date"/>

<field name="packageNumCredit" class="java.lang.String"/>

<field name="sellerplaceCode" class="java.lang.String"/>

<field name="sellerplaceTypeText" class="java.lang.String"/>

..................................................................................................................

just in case you would like specific example.

Thank you in advance

[2843 byte] By [samfuertea] at [2007-11-27 10:12:44]
# 1

what approach are u using? DOM or SAX?

@@CKM@@a at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 2

I use both.

samfuertea at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 3

JDOM is a nice and easy way to do what you want. Read more about JDOM on www.jdom.org.

asgshea at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 4

in dom you have all the APIs available for fetching the attributes of the node... try that... read the docs for org.w3c.dom...

@@CKM@@a at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 5

> I have made an xml parser and now I want to parse

> only some specific elements from xml file according

> to another txt file (datasource),

Step 1: First read all the fields(element names) from txt file into an Set.

Step 2: Make this set available to the parser.

Step 3: During parsing check whether this node has to processed by checking the object ( element name ) in the collection (set).

Step 4: If element is present in the collection then execute the logic else neglect it.

I Hope that the above idea will help you.

Vishwas_Prasannaa at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 6

Why is everyone telling the OP to use other XML parsers to make his own parser work?

georgemca at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 7

that's a good question. actually I prefer to do it by myself, but anyway, all opinions are welcome.

samfuertea at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 8

Take a look at Java Architecture for Xml Binding (JAXB) instead... it binds your XML to a Java object so creating and reading XML is as easy as creating a JAVA object...

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

c0demonk3ya at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 9

> Take a look at Java Architecture for Xml Binding

> (JAXB) instead... it binds your XML to a Java object

> so creating and reading XML is as easy as creating a

> JAVA object...

>

> http://java.sun.com/developer/technicalArticles/WebSer

> vices/jaxb/

Or Apache Digester. Or xml-beans. Or Castor

georgemca at 2007-7-28 15:22:12 > top of Java-index,Java Essentials,Java Programming...
# 10

> Why is everyone telling the OP to use other XML

> parsers to make his own parser work?

Because he doesn't need to reinvent the wheel when there is plenty of good technology already out there

c0demonk3ya at 2007-7-28 15:22:13 > top of Java-index,Java Essentials,Java Programming...
# 11

> > Why is everyone telling the OP to use other XML

> > parsers to make his own parser work?

>

> Because he doesn't need to reinvent the wheel when

> there is plenty of good technology already out there

But he's already re-invented it. His question is how to get it to work. More suitable advice would be "don't bother, just use PARSER-X", not "use this parser to get your parser to work" - that's crazy talk

georgemca at 2007-7-28 15:22:13 > top of Java-index,Java Essentials,Java Programming...
# 12

> But he's already re-invented it. His question is how

> to get it to work. More suitable advice would be

> "don't bother, just use PARSER-X", not "use this

> parser to get your parser to work" - that's crazy talk

Fair point... but I only mentioned JAXB as he said all opinions were welcome and obviously wanted more advice on what to use

So "don't bother, just use JAXB instead"

:D

c0demonk3ya at 2007-7-28 15:22:13 > top of Java-index,Java Essentials,Java Programming...
# 13

> > But he's already re-invented it. His question is

> how

> > to get it to work. More suitable advice would be

> > "don't bother, just use PARSER-X", not "use this

> > parser to get your parser to work" - that's crazy

> talk

>

> Fair point... but I only mentioned JAXB as he said

> all opinions were welcome and obviously wanted more

> advice on what to use

>

> So "don't bother, just use JAXB instead"

>

> :D

Reasonable enough. OP, why are you bothering to write your own parser anyway?

georgemca at 2007-7-28 15:22:13 > top of Java-index,Java Essentials,Java Programming...
# 14

cause I did it so far, but if I can use a parser from ou there which can do what I want, then I guess it's much better.

FYI I'm not actually a java developer (I have worked only with web apps and servlets in java long time ago - which is different than this).

So, I'm not so familiar with the paresrs like xerces or dom or whatever. And I don't know exactly how they "work" and what they can do.

So I made my own parser which gets the xml nodes and their values and saves to xml file. But now I need to "read" another datasource (txt file) - cmpare these two and send the comparison's results to the output.

So as you realize, I'm a little confused (and lost) right now :D

samfuertea at 2007-7-28 15:22:13 > top of Java-index,Java Essentials,Java Programming...
# 15

Help guys,

I'm still confused about that. First things first I think is the right way.

So, first question is how am I gonna get only the "field" NodeName from the xml file:

i.e. <field name="f10" class="java.lang.String" />

I don't care about parsing the rest of the xml file, let's say I want only these nodes.

Thanks

samfuertea at 2007-7-28 15:22:17 > top of Java-index,Java Essentials,Java Programming...