• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing XML string using Java Program

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can use castor www.castor.org ,you can use this for parser,try it very very simple.
 
Raghuraman Muthuswamy
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use the String's indexOf and substring methods.
 
Ranch Hand
Posts: 225
IBM DB2 Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The official language of JavaRanch is English. Posts not in English may be deleted without warning.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ....
 
reply
    Bookmark Topic Watch Topic
  • New Topic