Parsing...

Hi, I am new to XML and want to know how to parse if I have something like this "<ROW ACC_NO="10802001" REFR_NO="0443310043" TRAN_TYP="TR" TRD_PX=".00000" > </ROW>" in my input xml file. Urgent .. PlSumanth
[259 byte] By [sumanth_kumar] at [2007-9-26 3:08:12]
# 1

Sumanth,

you'd try something like this:

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.NamedNodeMap;

import org.xml.sax.SAXException;

class Test{

public void domParse(String url)

{

DocumentBuilder parser;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

//factory.setValidating(true);

try {

parser = factory.newDocumentBuilder();

Document doc = parser.parse(url);

Node svg = doc.getElementsByTagName("ROW").item(0);

if(svg!=null)

{

NamedNodeMap nnmap = svg.getAttributes();

if(nnmap!=null)

{

int n=nnmap.getLength();

for(int i=0;i<n;++i)

{

Node node=nnmap.item(i);

System.out.println("attibute("+node.getNodeName()+")="+node.getNodeValue());

}

}

else

System.out.println("NamedNodeMap =null");

NodeList svg_children=svg.getChildNodes();

int n=svg_children.getLength();

for(int i=0;i<n;++i)

{

Node node=svg_children.item(i);

System.out.println("element("+node.getNodeName()+") type="+node.getNodeType()+" txt=["+node.getNodeValue()+"]");

}

}

else

System.out.println("Node ROW=null");

} catch (Exception e) {

e.printStackTrace();

}

}

static public void main(String[] args)

{

Test x=new Test();

x.domParse("test.xml");

}

}

this is my test.xml:

><?xml version="1.0"?>

<ROW ACC_NO="10802001" REFR_NO="0443310043" TRAN_TYP="TR" TRD_PX=".00000" > </ROW>

Good luck.

lk555 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Hi, at the run time i got MalformedURLException at "x.domParse("test.xml");"sumanth
sumanth_kumar at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
That's weird. Works fine for me (win95, jdk1.3, xerces 1.4.1).
lk555 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
I fixed that prob. It is a Jar file problem.. Thanks
sumanth_kumar at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Hi Leo, I have more than 100 rows like that one file and want to get all the node values .. how i can go thru entire file... Thanks
sumanth_kumar at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

I guess I have to work harder for these Duke Dollars ;-).

Ok, try this:

class Test{

public void domParse(String url)

{

DocumentBuilder parser;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {

parser = factory.newDocumentBuilder();

Document doc = parser.parse(url);

NodeList rowChildren = doc.getElementsByTagName("ROW");

if(rowChildren!=null)

{

int n=rowChildren.getLength();

for(int i=0;i<n;++i)

{

System.out.println("- Row Element ("+i+") --");

Node childNode=rowChildren.item(i);

NamedNodeMap nnmap = childNode.getAttributes();

if(nnmap!=null)

{

int nAtts=nnmap.getLength();

for(int j=0;j<nAtts;++j)

{

Node node=nnmap.item(j);

System.out.println("attibute("+node.getNodeName()+")="+node.getNodeValue());

}

}

else

System.out.println("NamedNodeMap =null");

}

}

else

System.out.println("no row-children found");

} catch (Exception e) {

e.printStackTrace();

}

}

static public void main(String[] args)

{

Test x=new Test();

x.domParse("test.xml");

}

}

Good luck.>

lk555 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
Thanks .. it is good man
sumanth_kumar at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
Sumanth,don't forget to take a look at this: http://forums.java.sun.com/rewardFaq.jsp#assign;-)
lk555 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9

Hello lk555:

I have kindda similar problem as Sumanath, my XML file looks like that:

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web>

<property-name>Sybase Server</property-name>

<property-value>OMSDEVL</property-value>

<property-name>RDBMS</property-name>

<property-value>librarydb</property-value>

<property-name>table-name</property-name>

<property-value>librarydb..BOOKS_MEDIA</property-value>

<property-name>csv-file</property-name>

<property-value>library.txt</property-value>

<property-name>format-file</property-name>

<property-value>library.fmt</property-value>

</web>

I used your code but I got errors...

can you help please...

Ahmad

abilal71 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10

Ahmad,

Sumanth wanted to get data out of element attributes. With getAttributes() he'd get a NamedNodeMap where each attribute's content is part of the Node accessed by .item(i)

In your case, the content you're interested in is not stored in an attribute but in the element itself. The DOM spec (http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/) however, requires that the content of an element be stored in a (text-) child node of that element. That particular point causes a lot of confusion for newbies to XML as you can see by browsing through this forum.

In short, by using getNodeValue() on an element you'd get null back, you will find the content of an element in its child node(s).

So, you should be doing something like this:

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

class Test{

public void domParse(String url)

{

DocumentBuilder parser;

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

try {

parser = factory.newDocumentBuilder();

Document doc = parser.parse(url);

Node web = doc.getElementsByTagName("web").item(0);

if(web!=null)

{

NodeList webChildren=web.getChildNodes();

int n=webChildren.getLength();

for(int i=0;i<n;++i)

{

Node node=webChildren.item(i);

if(node.getNodeType()==Node.ELEMENT_NODE)

{

NodeList nodeChildren=node.getChildNodes();

int m=nodeChildren.getLength();

System.out.println("element ><"+node.getNodeName()+">");

for(int j=0;j<m;++j)

if(nodeChildren.item(j).getNodeType()==Node.TEXT_NODE)

System.out.println(" txt=["+nodeChildren.item(j).getNodeValue()+"]");

System.out.println("");

}

}

}

else

System.out.println("element ><web> not found");

} catch (Exception e) {

e.printStackTrace();

}

}

static public void main(String[] args)

{

Test x=new Test();

x.domParse("test.xml");

}

}

Good luck.

lk555 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11
Thank you lk555:Thank you and God Bless you....Ahmad
abilal71 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 12

Dear lk555:

If I am reading a XML file and parsing it then I should do it this way....tell me if i am wrong

All the above and in the main

public static void main(String[] args) {

Test atest = new Test();

atest.domParse("c:/temp/web.xml");

}

OR

public static void main(String[] args) throws Exception {

Buffered Reader bf = new BufferedReader(new FileReader("c:/temp/web.xml"));

String s;

while ((s=bf.readLine()) != null) {

System.out.println(s);

}

}

Can you tell me using the above code...

Ahmad

}

abilal71 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 13

I don't fully understand your question since your second alternative just reads and displays a file.

The line parser.parse(url) parses the XML file. There are various input sources you can call ".parse" with. Take a look at the documentation of JAXP's DocumentBuilder.parse methods at:

http://java.sun.com/xml/jaxp-1.1/docs/api/

Hope that helps.

P.S.

See also: http://forums.java.sun.com/rewardFaq.jsp#assign

lk555 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 14

Hello lk555:

Thank you I understand that part but I am getting error when I complie.....

5 errors

errorlevel=1

Compilation Failed

C:\Testing>prepare

Test.java:1: cannot resolve symbol

symbol : class DocumentBuilder

location: package parsers

import javax.xml.parsers.DocumentBuilder;

^

Test.java:2: cannot resolve symbol

symbol : class DocumentBuilderFactory

location: package parsers

import javax.xml.parsers.DocumentBuilderFactory;

^

Test.java:13: cannot resolve symbol

symbol : class DocumentBuilder

location: class Test

DocumentBuilder parser;

^

Test.java:14: cannot resolve symbol

symbol : class DocumentBuilderFactory

location: class Test

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

^

Test.java:14: cannot resolve symbol

symbol : variable DocumentBuilderFactory

location: class Test

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

^

5 errors

errorlevel=1

Compilation Failed

MY .BAT LOOKS LIKE THIS

@echo off

set PATH=%PATH%;d:\jdk1.3\bin

set XERCES_HOME=d:\Apache\Xerces-J-bin.1.4.1

set CLASSPATH=%CLASSPATH%;d:\jdk1.3\lib\j2ee.jar;%XERCES_HOME%\xerces.jar;

javac -d c:\Testing Test.java

if %errorlevel%==0 goto Label1

if not %errorlevel%==0 goto Label2

:Label1

echo errorlevel=%errorlevel%

echo Compiled Successfully

goto End

:Label2

echo errorlevel=%errorlevel%

echo Compilation Failed

goto End

:End

CAN U SUGGEST SOMETHING

THANKS

AHMAD

abilal71 at 2007-6-29 11:12:47 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 15

looks like javac can't find the JAXP package. It is part of Xerces1.4 versions. If you're using older version, go get it from xml.apache.org.

I'm not familiar with the j2ee.jar file. Maybe it contains XML stuff, so try and put your xerces.jar in front of j2ee.jar. Make sure that jar files in your jdk/lib/ext don't interfere (jar files in there are checked before stuff you specify in your classpath.

Good luck.

lk555 at 2007-7-1 3:22:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 16

Hi Sumanath:

Can you tell me how did fix your issue with the jar file, because I am having the same problem.

C:\Testing>prepare

Test.java:1: cannot resolve symbol

symbol : class DocumentBuilder

location: package parsers

import javax.xml.parsers.DocumentBuilder;

^

Test.java:2: cannot resolve symbol

symbol : class DocumentBuilderFactory

location: package parsers

import javax.xml.parsers.DocumentBuilderFactory;

^

Test.java:13: cannot resolve symbol

symbol : class DocumentBuilder

location: class Test

DocumentBuilder parser;

^

Test.java:14: cannot resolve symbol

symbol : class DocumentBuilderFactory

location: class Test

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

^

Test.java:14: cannot resolve symbol

symbol : variable DocumentBuilderFactory

location: class Test

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

^

5 errors

errorlevel=1

Compilation Failed

Can anybody guide me?

AB

abilal71 at 2007-7-1 3:22:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 17

hi lk555:

I got compiled successfully, but I am getting this error

C:\Testing>java Test

java.net.NoRouteToHostException: Operation timed out: no further information

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:312)

at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:125)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:112)

at java.net.Socket.<init>(Socket.java:273)

at java.net.Socket.<init>(Socket.java:100)

at sun.net.NetworkClient.doConnect(NetworkClient.java:50)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:331)

at sun.net.www.http.HttpClient.openServer(HttpClient.java:517)

at sun.net.www.http.HttpClient.<init>(HttpClient.java:267)

at sun.net.www.http.HttpClient.<init>(HttpClient.java:277)

at sun.net.www.http.HttpClient.New(HttpClient.java:289)

at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:379)

at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.ja

va:472)

at java.net.URL.openStream(URL.java:798)

at org.apache.xerces.readers.DefaultReaderFactory.createReader(DefaultReaderFactor

y.java:149)

at org.apache.xerces.readers.DefaultEntityHandler.startReadingFromExternalEntity(D

efaultEntityHandler.java:767)

at org.apache.xerces.readers.DefaultEntityHandler.startReadingFromExternalSubset(D

efaultEntityHandler.java:566)

at org.apache.xerces.framework.XMLDTDScanner.scanDoctypeDecl(XMLDTDScanner.java:11

39)

at org.apache.xerces.framework.XMLDocumentScanner.scanDoctypeDecl(XMLDocumentScann

er.java:2197)

at org.apache.xerces.framework.XMLDocumentScanner.access$000(XMLDocumentScanner.ja

va:86)

at org.apache.xerces.framework.XMLDocumentScanner$XMLDeclDispatcher.dispatch(XMLDo

cumentScanner.java:776)

at org.apache.xerces.framework.XMLDocumentScanner.parseSome(XMLDocumentScanner.jav

a:381)

at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:952)

at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:123)

at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:183)

at Test.domParse(Test.java:18)

at Test.main(Test.java:59)

Anybody can help.........

Ahmad

abilal71 at 2007-7-1 3:22:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 18
Well, looks like you don't have an internet connection when parsing. Remember, you're using an external DTD located on some sun server. Even when validation is turned off, the parser validates URI of the DTD location and parses the DTD file.
lk555 at 2007-7-1 3:22:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 19

Hello lk555:

My XML file looks like this and I just need to display the value tag in the output....

<properties>

<property>

<name>Sybase Server</name>

<value>OMSDEVL</value>

</property>

<property>

<name>Database</name>

<value>librarydb</value>

</property>

<property>

<name>table-name</name>

<value>BOOKS_MEDIA</value>

</property>

<property>

<name>csv-file</name>

<value>library.txt</value>

</property>

<property>

<name>format-file</name>

<value>library.fmt</value>

</property>

</properties>

Can you please help me with it like you did before

Thanks a lot

ab

abilal71 at 2007-7-1 3:22:13 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...