Parsing XML and storing values in HashMap
843834May 17 2007 — edited May 22 2007Hai 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();
}
}
}
****************************