-
Reading from XML file
Hallo
I have written the following source code to read out from a XML file; and actualle line which are between <Line> </LINE> .
The problem is when there are more than one line of <Line> </LINE> . in sequence I get just the first one, why!?
- <SPEECH>
<SPEAKER>MARCELLUS</SPEAKER>
<LINE>Horatio says 'tis but our fantasy,</LINE>
<LINE>That if again this apparition come,</LINE>
<LINE>He may approve our eyes and speak to it.</LINE>
</SPEECH>
Second problem that I'm facing is when I try to use the hash map to count the occurrences of the words, I get an error message for the command:
anInteger = new Integer(Integer.parseInt(anInteger + 1));
//"The operato + is undefined for the argument type Integer, int"
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import java.util.StringTokenizer;
import java.util.Hashtable;
import java.util.Map;
import java.util.HashMap;
import java.util.WeakHashMap;
import java.util.TreeMap;
import java.util.Iterator;
import java.lang.Integer;
public class HAMLET {
public static void main(String[] args) {
Map map = new HashMap(); // hash table
map = new TreeMap();
//open the file and read the "LINE" lines
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("c:/hamlet.xml"));
doc.getDocumentElement ().normalize ();
NodeList listOfLines = doc.getElementsByTagName("SPEECH");
int r = listOfLines.getLength();
for(int s=0; s<listOfLines.getLength() ; s++){
Node firstLineNode = listOfLines.item(s);
if(firstLineNode.getNodeType() == Node.ELEMENT_NODE){
Element firstLineElement = (Element)firstLineNode;
NodeList firstNameList = firstLineElement.getElementsByTagName("LINE");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
String LineofFile = ((Node)textFNList.item(0)).getNodeValue().trim();
word_by_word_extraction linedecom = new word_by_word_extraction();
String wordsofline[] = linedecom.wordextraction(LineofFile);
int numwordline = wordsofline.length;
for(int gh = 0; gh><numwordline; gh++)
{
//PorterStemmer is a function which gives back the stemm of a string
PorterStemmer termto = new PorterStemmer();
String returenedterm = termto.stem(wordsofline[gh]);
// ubdating the hash MAP
Integer anInteger ;
StringTokenizer st = new StringTokenizer(returenedterm);
if (map.containsKey(returenedterm)) // key already in Map
{
anInteger = (Integer)map.get(returenedterm);
//"The operato + is undefined for the argument type Integer, int"
anInteger = new Integer(Integer.parseInt(anInteger + 1));
}
else // key doesn't exist yet.
{
// Create new integer indicating one occurrence.
anInteger = new Integer(1);
}
}
}
}
}
catch (Throwable t) {
t.printStackTrace ();
}
// writing out the hash table
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(map.get(s) + "\t" + s);
}
}>
Similar Threads
-
Replies: 0
Last Post: 10-28-2002, 06:25 AM
-
By Andrew McLellan in forum Java
Replies: 3
Last Post: 05-09-2001, 05:34 PM
-
By Kelvin Teh in forum XML
Replies: 3
Last Post: 02-16-2001, 10:09 AM
-
By CHRISTOS STAVRINOU in forum VB Classic
Replies: 0
Last Post: 11-16-2000, 04:58 PM
-
By CHRISTOS STAVRINOU in forum Database
Replies: 0
Last Post: 11-16-2000, 04:56 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks