Parsing XML and storing values in HashMap

Hai I am parsing this XML file using SAX parser .I am getting the output. But how to store the ta and value in HaspMap as key /value pair.Can anyone help me please.

Thanks a lot,

selvan

*****************

CrsCreate.xml

*****************

<?xml version="1.0" encoding="UTF-8"?>

<!-- $Id: CrsCreate.xml,v 1.2 2005/04/14 18:34:35 arburga Exp $ -->

<SERVICE_REQUEST ACTION="request"

CLIENT_APP="Integrator"

REF_ID="20093454"

TRANSACTION_ID="20093454">

<SERVICE>CRS</SERVICE>

<REQUEST>PVC_CREATE</REQUEST>

<ADMIN_PVC ADMIN_STATUS=""

PVC_NAME="_"></ADMIN_PVC>

<ATM_EP>

<NAP>

<NODE_TYPE>CISCO_BPX</NODE_TYPE>

<CIRCUIT_ID>24/HFGJ/083308//ACSO</CIRCUIT_ID>

<NODE_NAME>DNVREAB0</NODE_NAME>

<SHELF></SHELF>

<SLOT>3</SLOT>

<PORT>10</PORT>

</NAP>

<VCC>

<VPI>1</VPI>

<VCI>32</VCI>

</VCC>

<VBR_NRT>

<PCR>94340</PCR>

<SCR>23585</SCR>

<MBS>100</MBS>

<CDVT>50</CDVT>

</VBR_NRT>

</ATM_EP>

<ATM_EP>

<NAP>

<NODE_TYPE>CISCO_BPX</NODE_TYPE>

<CIRCUIT_ID>24/OBGJ/760686//ACSO</CIRCUIT_ID>

<NODE_NAME>DNVREAB0</NODE_NAME>

<SHELF></SHELF>

<SLOT>9</SLOT>

<PORT>1</PORT>

</NAP>

<VCC>

<VPI>1</VPI>

<VCI>32</VCI>

</VCC>

<VBR_NRT>

<PCR>94340</PCR>

<SCR>23585</SCR>

<MBS>100</MBS>

<CDVT>50</CDVT>

</VBR_NRT>

</ATM_EP>

<VIRTUAL_CIRCUIT_TYPE>

<PREFERRED_VC VALUE="PVC"></PREFERRED_VC>

</VIRTUAL_CIRCUIT_TYPE>

</SERVICE_REQUEST>

**********************************************

*****************************

NewTrial.java

*****************************

import java.io.*;

import javax.xml.parsers.*;

import org.xml.sax.HandlerBase;

import org.xml.sax.InputSource;

import org.xml.sax.XMLReader;

import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class NewTrial {

public static void main(String args[]) {

try {

SAXParserFactory factory = SAXParserFactory.newInstance();

SAXParser saxParser = factory.newSAXParser();

DefaultHandler handler = new DefaultHandler() {

boolean name = false;

public void startElement(String uri, String localName, String qName, Attributes attributes)

throws SAXException {

//System.out.println(""+localName);

System.out.println(""+qName);

}

public void endElement(String uri, String localName, String qName)

throws SAXException {

System.out.println(""+qName);

}

public void characters(char ch[], int start, int length) throws SAXException {

System.out.println(new String(ch,start,length));

/*if (name) {

System.out.println("Name: "

+ new String(ch, start, length));

name = false;

}*/

}

};

saxParser.parse("CrsCreate.xml", handler);

} catch (Exception e) {

e.printStackTrace();

}

}

}

****************************

[3804 byte] By [Selvan_RCa] at [2007-11-27 4:44:39]
# 1

What are you trying to do exactly?

May I suggest, check out xmlbeans http://xmlbeans.apache.org/

The idea is that for every object in your XML, a corresponding java class will exist, giving you a tree structure of java class representing your xml. That way, keeping track of your objects and values becomes easier.

Maybe then later on if you needed the Hashmap, it can be used with those objects. Storing key value pairs isn't hard. Check out the API. Simply...

Map myMap = new HashMap();

myMap.put (key_0, val_0);

myMap.put(key_1, val_1);

kdajania at 2007-7-12 9:56:42 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hai kdajani ,

Thanks, I have to fetch the values from the XML elements and to store the tag name as key and element value as value in HashMap .It should be sent along a request.So the receiver can get the Hashmap object and he can use the values. I checked out xmlbeans also. Should i have to write a seperate java class for creating the tree structure?.

Selvan_RCa at 2007-7-12 9:56:42 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
Hi,No you don't to write it. There is a command line tool where, given an xsd file, will create all the classes for you. So as long as you have the xsd for your schema, you're in good shape.
kdajania at 2007-7-12 9:56:42 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
Hi kdajani ,Thank you,i'll try it now.
Selvan_RCa at 2007-7-12 9:56:42 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Good luck
kdajania at 2007-7-12 9:56:42 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...