aspose file tools*
The moose likes Java in General and the fly likes Parsing XML string using Java Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing XML string using Java Program" Watch "Parsing XML string using Java Program" New topic
Author

Parsing XML string using Java Program

Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Hello All,
I am trying to Parse an XML file which i converted into string using Java IO files. Now the XML String has to be parsed by using Java Application Program, that is without using DOM or SAX parsers. How can i do it without using DOM or SAX. If you can suggest me some tutorial or code snipped i can develop the idea. Your reply is very much appreciated.

Thanks in advance,

Raghu
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Is there a reason why you are trying to parse XML without using a parser? No matter, I'll assume there is. Your only route in this situation would be to...write a parser. You might condiser the java.util.regex package if you must do this.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Paul,
Thanks for your reply. I really dont know much about util.regex. Is there any tutorial to Parse a string using regex. If so please provide me the link.

Thanks in advance,

regards,
Raghu
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Well, the internet is full of sources which explain Regular Expressions. Google a while and you'll get plenty.

I still don't understand why you don't just use the parsers provided for you though. Much, much easier.
[ May 05, 2005: Message edited by: Paul Sturrock ]
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
Hi,
Even if your XML file has your own custom tags, using a XML parser you can parse it. Since few people think that, only standard XML files can be parsed using SAX & DOM, i'm telling this.

If that is not the reason, then leave it.


Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I am assuming this is some type of homework assignment, which is why you can't use a parser...

If you know what xml tags you are looking for, you can use the String method indexOf to locate the tag. Then use indexOf again to find the end tag and everything in between the tags is your data. Using the two indexes you found you can use the String substring method to pull the text out from betwen the tags.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Thanks Paul,
It seems using regex is the best way to parse without SAX or DOM. I searched the net but i couldnt get code snippet of how to do this. If you can provide me with the code i can learn easily. Just give me a sample to head start in this topic.

Thanks in advance,

regards,
Raghu
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Hello,
This is the XML file i have to parse manually(without using DOM or SAX). If you can provide piece of code i can develop the idea. Anxiously waiting for your reply.

With best regards,
Raghu
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Hello,
This is the XML file i have to parse manually(without using DOM or SAX). If you can provide piece of code i can develop the idea. Anxiously waiting for your reply.

<xml>
<FAML >
<HEADER>
<REQID>ACCT_VALID</REQID>
<SCODE>IVR</SCODE>
<XREF>200502171335461</XREF>
<SYSTEMID>*</SYSTEMID>
</HEADER>
<REQUEST>
<ACCNO>1234567</ACCNO>
<TPIN>1234</TPIN>
</REQUEST>
</FAML >
</xml>


With best regards,
Raghu
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
there are various source free DOM parsers available on the internet.

try the following link

Dom Source Code

i think you will find all the code help you need.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Nick,
I want to parse the XML file without using DOM or SAX or any other API. I just want to parse the file manually. To do this i have converted the XML file to String using java io. Now i have to parse this String(contains XML) without using SAX or DOM.

regards,
Raghu
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
RaghuRaman,

What Niki meant was that, you can get the source code from the link mentioned.

Once you get the source code of how SAX and DOM parsers parse XML, you will get a very clear picture as to how you could do it and you could use that code as reference.

if you only want to prse the XML file, i recommend you look at SAX parser codes to get an idea as to how it is done.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
What exactly do you have to do? Parsing an XML file means reading it. Parsers don't do much of anything by themselves other than check for well-formedness (if there is such a word). Are you supposed to find a particular piece of data and display it in some way? Or are you supposed to determine if the XML file is well-formed?
Faris A Saduli
Ranch Hand

Joined: Feb 22, 2005
Posts: 58
Hi,

You can use castor www.castor.org ,you can use this for parser,try it very very simple.
Raghuraman Muthuswamy
Ranch Hand

Joined: Mar 18, 2003
Posts: 73
Dear Thomas,
Thanks for your reply. The task i have to do with the XML document is I have to parse the XML file and fetch the Tag values and Attribute name and value.

regards,
Raghu
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I would use the String's indexOf and substring methods.
Ganesh Gowtham
Ranch Hand

Joined: Mar 30, 2005
Posts: 225

hi raghu

intha code use pannu pa ,
edhu xml files irundhu data edukum



import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.w3c.dom.*;
//import org.apache.xerces.parsers.DOMParser;
/**
*
* <font color="red" size="3" face="Monotype Corsiva">Core class to parse the Xml data</FONT> <BR>
* <font color="green" size="3" face="Monotype Corsiva">@author (Gowtham G R)</FONT> <BR>
* <font color="violet" size="3" face="Monotype Corsiva">@version (a version number or a date)</FONT> <BR>
*/
public class Xml
{
static int employeeCount = 0;
static Node employeeNodeName = null;
static NodeList employeeNode = null;
static DocumentBuilderFactory factory = null;
static DocumentBuilder builder = null;
static Document doc = null;

static
{
try
{
String xmlFile = "file:\\d:\\myjava\\config.xml";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
doc.getDocumentElement ().normalize ();
employeeNode = doc.getElementsByTagName("ext");
employeeCount = employeeNode.getLength();
}
catch (FactoryConfigurationError e)
{
System.out.println("FactoryConfigurationError"+e.getMessage());
}
catch (ParserConfigurationException e)
{
System.out.println("ParserConfigurationException"+e.getMessage());
}
catch (SAXException e)
{
System.out.println("SAXException"+e.getMessage());
}
catch (IOException e)
{
System.out.println("IOException"+e.getMessage());
}
}
/**
*mtd to parse the Node from Dom Tree and return the extension pertaining to this Node
*
* @param nodeName the index of the desired character.
* @return the desired character.
* @exception StringIndexOutOfRangeException
*/
static String doGetExtensionFromNode(String nodeName)
{
String resultantString = "";
for(int x =0 ;x < employeeCount ; x++)
{
Node employeeNodeName = employeeNode.item(x).getChildNodes().item(0);
if(employeeNodeName.getParentNode().getParentNode().getNodeName() == nodeName)
resultantString = employeeNodeName.getNodeValue();
else if(employeeNodeName.getParentNode().getParentNode().getNodeName() == nodeName)
resultantString = employeeNodeName.getNodeValue();
else if(employeeNodeName.getParentNode().getParentNode().getNodeName() == nodeName)
resultantString = employeeNodeName.getNodeValue();
}
return (resultantString);
}
}


Thanks
gowtham
grgowtham_cse@yahoo.com


Thanks, Ganesh Gowtham
http://ganesh.gowtham.googlepages.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The official language of JavaRanch is English. Posts not in English may be deleted without warning.


[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It really is easy to parse simple cases yourself. I did this in REXX not long ago cause I didn't have a parser. If your XML is as shown before ...

The first character must be <
Use indexOf to find >
Use substring to get the tag
Use indexOf to find </tag>
Use substring to get the value between the open & close tags
Store tag & value somewhere
The next character should be < or end of input
That looks familiar, go back to line 2.

If you get attributes, namespaces and more advanced XML things get trickier, but your example should be easy enough.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Of course that simple example left out nested tags. Took me overnight to realize that. If the "value" you get out of the XML also starts with < it may be time for recursion.

Was this answering the right question?
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
I wish Javaranch shouldnt be a place where you get your assignments done.

Problems and Solutions and Designs can be shared, but asking for a specific source code, i dont think is the right thing to do !!!

Just give a try to your programming skills ....
 
 
subject: Parsing XML string using Java Program