• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

XML to HashMap......

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I know this is a silly problem but i am having trouble to figure out how to convert an xml file into an HashMap.
My XML looks like this.
<xml>
<testcase_0001>
<ani>8005551005</ani>
<dnis>8005551002</dnis>
<params>
<called_num>
<rec_fmt_number>87</rec_fmt_number>
<trunk_group>987654</trunk_group>
</params>
</testcase_0001>
<testcase_0002>
<ani>8005551010</ani>
<dnis>8005551005</dnis>
<params>
<called_num>
<rec_fmt_number>97</rec_fmt_number>
<trunk_group>987634</trunk_group>
</params>
</testcase_0002>
<testcase_0003>
<ani>8005551056</ani>
<dnis>8005551020</dnis>
<params>
<called_num>
<rec_fmt_number>870</rec_fmt_number>
<trunk_group>9876</trunk_group>
</params>
</testcase_0003>
</xml>
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the pseudocode -
[1] Parse the XML using DOM.
[2] Get the child nodes of root- you will get a NodeList
[3] Get the iterator for the NodeList.
[4] Iterate through each node.
[5] Append node and value to your hashmap.

Give it a shot!
 
Praveen Dharmavaram
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heres what i do.
1). I get the root node
2). Check if it has a child node.
3). if it does then i iterate
4). otherwise i get the node name and value and add it to a hash table.
But the problem is i dont get the exact tree and also i am appending the values of the nodes which have child nodes.
My hash map looks like
{ani=8005551010, trunk_group=987634, dnis=8005551005, rec_fmt_number=97, testcase_0002=
, params=
}
I dont want to append the values of testcase_0002 and params to my hashmap. Any ideas how to get that
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dpbhyd,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/**
Source * Http://www.JavaCommerce.com
Updated to prepare Hashtable from XML String by msandeepk@gmail.com
* This program Updated by msandeepk@gmail.com
**/

import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;

import org.xml.sax.*;

import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

public class BooksLibrary2 extends HandlerBase
{
protected static final String XML_FILE_NAME = "e:\\library2.xml";

public static void main (String argv [])
{
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Set up output stream
out = new OutputStreamWriter (System.out, "UTF8");

// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(XML_FILE_NAME), new BooksLibrary2() );

} catch (Throwable t) {
t.printStackTrace ();
}
System.out.println("Exiting");

System.out.println("Exited");
for(int j=0;j<htarr.length;j++){
if(htarr[j]!=null){
Enumeration e=htarr[j].keys();
while(e.hasMoreElements()){
String key=(String)e.nextElement();
System.out.println(key+":"+htarr[j].get(key));
}
System.out.println("______");
}
}

System.exit (0);
}

static private Writer out;

//===========================================================
// Methods in SAX DocumentHandler
//===========================================================

public void startDocument ()
throws SAXException
{
showData ("<?xml version='1.0' encoding='UTF-8'?>");
newLine();
}

public void endDocument ()
throws SAXException
{
try {
newLine();
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}

public void startElement (String name, AttributeList attrs)
throws SAXException
{
if(name.equalsIgnoreCase("Request")){

}else if(name.equalsIgnoreCase("Req_id")){
ht=new Hashtable();
}else{
this.Key=name;
}
showData ("**"+name+"**");
if (attrs != null) {
for (int i = 0; i < attrs.getLength (); i++) {
// showData (" ");
ht.put(attrs.getName(i),attrs.getValue((i)));
showData ("**"+attrs.getName(i)+"=\""+attrs.getValue (i)+"\"**");
}
}
// showData (">");
}

public void endElement (String name)
throws SAXException
{
if(name.equalsIgnoreCase("Req_id")){
this.htarr[counter++]=ht;
ht=null;
}
showData (name);
}

public void characters (char buf [], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);

if(s.length()>1){
System.out.println(this.Key+"@@"+s);
ht.put(this.Key,s);
this.Key=null;
showData ("^^"+s+"^^");
}

}

//===========================================================
// Helpers Methods
//===========================================================

// Wrap I/O exceptions in SAX exceptions, to
// suit handler signature requirements
private void showData (String s)
throws SAXException
{
try {
System.out.println("---"+s+"---");
// out.write (s);
out.flush ();
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}

// Start a new line
private void newLine ()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
// System.out.println("---"+lineEnd+"---");
out.write (lineEnd);
} catch (IOException e) {
throw new SAXException ("I/O error", e);
}
}
String Key=null;
String Value=null;
static Hashtable ht=null;
static Hashtable htarr[]=new Hashtable[10];
int counter =0;
}
 
money grubbing section goes here:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic