Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

XML Parsing through Java

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

I am trying to parse an XML File some of lines are as follows

<Column Name="PROD_LINE_NAME">REFRIGERATOR</Column>
<Column Name="FINAL_PRD_DATE">2006-05-16</Column>
<Column Name="RELEASE_CODE">V</Column>

If I am using DOM Parser then It is not a problem for me.
But The XML File is very large in size, so in that case I do not think that it will be wise to use DOM Parser.
So I am trying to use SAX Parser.

BY using SAX, I am able to read the value of the element like :- REFRIGERATOR,2006-05-16,V
But I am unable to read th value of those attribute like :- PROD_LINE_NAME,FINAL_PRD_DATE,RELEASE_CODE

Some code snippet is as follows :-

document = new SAXReader.read(xmlFile);
List allNodes = document.selectNodes("Column");
Iterator itrNodes = allNodes.iterator();
while (itrNodes.hasNext()) {
Node individual = (Node) itrNodes.next();
System.out.println("Nodes are "+individual.getStringValue());
}

So can anyone help me regarding this.

Thanks and Regards
Rajesh Swain
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any particular reason that you are not using API's like JDOM. They have good coverage for parsing various components of XML string.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If some Nodes are really Elements, you can use the attribute() method to get the values you want.

You're using a SAX parser under the covers, but isn't what I'd call SAX parsing. It still builds a document tree DOM and you have to wade through that to get anything done.

Have you done SAX parsing with the regular Handler interfaces? You get the attributes as arguments in that style. Don't know if that would be appropriate to your problem, but it is my first choice when I have a choice.
 
Rajesh Kumar Swain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by satsranchuser:
Any particular reason that you are not using API's like JDOM. They have good coverage for parsing various components of XML string.



Yes I am bound to USe sax parser as The XML file is of size around some 100 MB. So If I will use DOM then it may give OutOfMemory Error.
So please suggest what to do ?

I need some sample code for that.

Thanks
 
sathish kumar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class JDOMExample {
public void getXMLData() {
SAXBuilder sbldr = new SAXBuilder();
Document doc = null;
try {
doc =
sbldr.build(JDOMExample.class.getResourceAsStream("/jdom.xml"));
List lst = doc.getRootElement().getChildren();
for (Iterator iter = lst.iterator(); iter.hasNext();) {
Element element = (Element) iter.next();
List attrlist = element.getAttributes();
for (Iterator iterator = attrlist.iterator();
iterator.hasNext();
) {
System.out.println(
((Attribute) iterator.next()).getValue());
}
List contentlist = element.getContent();
for (Iterator iterator = contentlist.iterator();
iterator.hasNext();
) {
Content contentelement = (Content) iterator.next();
System.out.println(contentelement.getValue());
}
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Let me know if this helps
 
Rajesh Kumar Swain
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks satish I got the solution.

what I was tryng is :-

List allNodes = document.selectNodes("Products/Product/DataSource");
//Node individual = (Node) itrNodes.next();
Element individual_1 = (Element) itrNodes.next();

INstead of element I was trying to type cast it to Node that's why I was not getting the getAttributeValue().

Now It has been solved.

Regards
Rajesh
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic