How to Reading XML File with the help of java and
-
I try to read xml file and try to compare it with VO(a simple java file with hard coded values). but i am not confident after writing code. so i am paste whole code here, pls try to check it and remove its problems.
All these java files put in Package with name test and xml in root.
file name : SiteVo.xml
<VOItems>
<CVOItem>
<siteName type="String">yahoo</siteName>
<siteId type="String">a001</siteId>
<impressions type="Integer">30</impressions>
<clicks type="Integer">20</clicks>
<actions type="Integer">5</actions>
<minEcpm type ="Integer">60</minEcpm>
<uniqueUsers type ="String">10</uniqueUsers >
</CVOItem>
</VOItems>
file name: TestFailure.java
package test;
import junit.framework.TestCase;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
public class TestFailure extends TestCase {
boolean mObjCompared = false;
boolean input = false;
boolean mObjComparedExpected = true;
Map mapSiteVo = null;
Map mapXmlVo = null;
Map combineMap = new HashMap();
String file_path = "SiteVo.xml";
public TestFailure(String add)
{
super();
}
public class SiteVO {
private String siteName = "";
private String siteId = "";
// private String users = "";
// private int clicks = 0;
// private int minEcpm= 0;
// private int actions = 0;
// private int impressions = 0;
public Map getMap(){
Map siteVomap = new HashMap();
siteVomap.put("siteName",new String("yahoo"));
siteVomap.put("siteId",new Integer(2));
// siteVomap.put("users",new String("10"));
// siteVomap.put("clicks",new Integer(20));
// siteVomap.put("minEcpm",new Integer(60));
// siteVomap.put("impressions",new Integer(30));
// siteVomap.put("actions",new Integer(5));
return siteVomap;
}
}
// Initilize test fixture variables
protected void setUp(){
try{
SiteVO sitevoObj = new SiteVO();
mapSiteVo =sitevoObj.getMap();
MediaParser mediaParserObj = new MediaParser(file_path);
mapXmlVo = mediaParserObj.parse();
System.out.println("qqqqqqqqqqqqq" );
// Map combineMap = new HashMap();
// combineMap.put(mmediaId,mapXmlVo);
Iterator itrmap =mapXmlVo.keySet().iterator();
System.out.println("eeeeeeeeee" );
while(itrmap.hasNext()){
System.out.println("hhhhhhhhhhhhhhhhhhh");
InputFile lObj = (InputFile)itrmap.next();
String s =lObj.getSiteId();
String sa = lObj.getMedia();
System.out.println("SSSSSSSSSSSSss " +s);
System.out.println("SSSaaaaaaass " +sa);
if( (sa.equals("00111"))&& (s.equals("s001"))){
System.out.println("jjjjjjjjjjjjj" );
combineMap = (Map)mapXmlVo.get(lObj);
}else
System.out.println("input criterai does not match" );
}
// }
//
}
catch(IOException a){
}
catch(SAXException a){
}
catch(ParserConfigurationException a){
};
}
public void testCompareMap(){
assertEquals(mapSiteVo,combineMap);
}
public boolean assertEquals(Map obj1,Map obj2){
System.out.println("in assert equals ");
Utility.equal( obj1, obj2);
return true;
}
public static Test suite()
{
TestSuite suite = new TestSuite();
suite.addTest(new TestFailure("comparing")
{
protected void runTest() {
testCompareMap(); }
});
return suite;
}
public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
}
File Name : MediaParser.java
package test;
import javax.xml.parsers.;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.;
import org.xml.sax.; // SAX classes used for error handling by JAXP
import java.io.;
import java.util.*;
public class MediaParser
{ String file= "";
MediaParser(String file){
this.file= file;
System.out.println("File "+file);
}
public Map parse() throws IOException, SAXException, ParserConfigurationException{
Map lmap = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
// get VOItems elements
NodeList lVOItemsList = document.getElementsByTagName("VOItems");
// Our document will contain only one VOItems so it can be get by 0 item
Element lElement = (Element)lVOItemsList.item(0);
//System.out.println("VOITems Name " + lElement.getNodeName());
//System.out.println("VOITems Name 123456 " + lElement);
// Parser all the VOItems from the parent node
NodeList lVOItemList = lElement.getElementsByTagName("CVOItem");
//System.out.println("VOITem count " + lVOItemList.getLength());
//System.out.println("NAME " + lVOItemList.item(0).getNodeName());
//System.out.println("NAME Len " + ((Element)lVOItemList.item(0)).getElementsByTagName("").getLength());
Map combineMap = new HashMap();
String media = "";
String siteid="";
for(int i=0;i<lVOItemList.getLength();++i)
{
// get ith VOItem
Element lVOItem = (Element)lVOItemList.item(i);
// Parse VOItem and make a Map object for its content
// System.out.println( "going12345678 " +lVOItem);
lmap = parseVOItem(lVOItem);
// for(int j=0;j<lVOItemList.getLength();j++){
media = lVOItem.getAttribute("mediaId");
System.out.println( "media " +media);
siteid = lVOItem.getAttribute("siteID");
InputFile input = new InputFile(media,siteid);
// combineMap.put( lVOItem.getAttribute("mediaId"),lmap);
combineMap.put(input,lmap);
System.out.println("VOITems 44444Name " + lVOItem.getAttribute("mediaId"));
// Parse each VOItem and prepare a Map for that
// }
}
return combineMap;
}
//
// Start
public static Map parseVOItem(Element element)
{
//System.out.println( "Node name 1" + element.getNodeName());
// System.out.println( "rrrrrrrrrrrrr " + element.getAttribute("mediaId"));
HashMap lMap = new HashMap();
// if(element.getAttribute("mediaId").equals("002")){
// define local variables
NodeList lNodeList = element.getElementsByTagName("");
// System.out.println( "came in method count " + lNodeList.getLength());
for(int i=0;i<lNodeList.getLength(); ++i)
{
Node lNode = lNodeList.item(i);
System.out.println( "Node name 2" + lNode);
VOItemChild aChildObj = parseVOItemChild(lNode);
if(aChildObj!=null)
lMap.put(aChildObj.getKey(),aChildObj.getValue());
}
// print all the values
print(lMap);
//}
return lMap;
}
// Parse a node and return VOItem
public static VOItemChild parseVOItemChild(Node node)
{
// <slotId type ="integer">01</slotId>
// define local variables
String lType= "String"; // Default type
String lValue = null;
Object lValueObj = null;
String lKey = "";
// get node name which is used like key
lKey = node.getNodeName(); //returns all the attributes
//System.out.println("name !! " + lKey);
// get string value
lValue = node.getFirstChild().getNodeValue();
// System.out.println("value777 !! " + lValue);
// parse value type by the "type" attribue
//Node lNode = node.getAttributes().item(0);
lType = ((Element)node).getAttribute("type");
//System.out.println("type !! " + lType );
//lNode.getFirstChild().getNodeValue();
// get wrapper object
lValueObj = wrapperObject(lType,lValue);
VOItemChild lVOItemChild = new VOItemChild(lKey,lValueObj);
return lVOItemChild;
}
// Prepare wrapper object based upon the type string and string value
protected static Object wrapperObject(String type , String value)
{
Object lObj=null;
if(type.equalsIgnoreCase("Integer"))
{
lObj = new Integer(value);
}
if(type.equalsIgnoreCase("Float"))
{
lObj = new Float(value);
}
if(type.equalsIgnoreCase("String"))
{
lObj = new String(value);
}
if(type.equalsIgnoreCase("Double"))
{
lObj = new Double(value);
}
return lObj;
// More will depend upon the requirement
}
public static void print(Map map)
{
if(map==null)
System.out.println("map is null");
Iterator lIt = map.keySet().iterator();
while(lIt.hasNext())
{
Object lObj = lIt.next();
System.out.println("key = " + lObj + " value =" + map.get(lObj));
}
}
}
File Name: VOItemChild.java
package test;
public class VOItemChild
{
protected String mKey = null; // Key of VOItem
protected Object mValue = null;
public VOItemChild(String key , Object value)
{
mKey = key;
mValue=value;
}
public String getKey()
{
return mKey;
}
public Object getValue()
{
return mValue;
}
public void setKey(String key)
{
mKey=key;
}
public void setValue(Object obj)
{
mValue=obj;
}
}
file Name : InputFile.java
package test;
public class InputFile {
private String media = "";
private String siteId = "";
public InputFile(String media, String siteId) {
this.media = media;
this.siteId = siteId;
}
/*
* @return Returns the siteId.
*/
public String getSiteId() {
return siteId;
}
/*
* @param siteId The siteId to set.
/
public void setSiteId(String siteId) {
this.siteId = siteId;
}
/*
* @return Returns the media.
/
public String getMedia() {
return media;
}
/*
* @param media The media to set.
*/
public void setMedia(String media) {
this.media = media;
}
}
file Name: Utility.java
package test;
import java.util.Iterator;
import java.util.Map;
public class Utility{
public static boolean equal(Map obj1,Map obj2){
System.out.println("in assert equals utility");
if(obj2==null)
{System.out.println("map is null");}
//String s=(String)mapXmlVo.get("siteId");
// if (s == "a001"){
Iterator lIt = obj2.keySet().iterator();
boolean check=true;
while(lIt.hasNext())
{ System.out.println("nnnnnnnnnnnnnnnnn");
Object lObj = lIt.next();
// System.out.println("mmmmmmmmmmmmmmmmmmmm");
// if(obj1.get(lObj).equals(obj2.get(lObj))){
// // System.out.println("HELLO HELLO" +obj1.get(lObj)+"fdfdfd" +(obj2.get(lObj)));
// // System.out.println("666666 Objetcs are equal");
// // return true;
// continue;
// }
// else{
// System.out.println("7777 Objetcs are not equal");
// check=false;
// break;
// //return false;
// }
//
//
if(obj1.equals(obj2)){
System.out.println("666666 Objetcs are equal");
return true;
}
else {
System.out.println("7777 Objetcs are not equal");
return false;
}
}
if(check==false)
return check;
else
return check;
}
}
Post a reply
Java forum discussion
-
Which is harder to learn Java or C++ ?
by surbhinahta (114 replies)
-
C ++ public int __cdecl printf (const char * __restrict__ _Format, ...) problem.
by sgameyta (0 replies)
-
What do you need to build a simulation software?
by ilenia.news (0 replies)
-
Taking a New Path
by samu13813 (0 replies)
-
What is Mhapks? how to open edit or modify APK files?
by eeshasajawal2004 (0 replies)
Quick links
Recent activity
- arif ahmad replied to How to receive data in web ...
- William Thompson replied to What is the name of the Win...
- Sameera Piyadigamage replied to Point of Sale Developers: H...
- Scott Carline replied to 4 x C# Developers for large...
- Rajendra Dhakal replied to Restore SQL Server text dat...
- cloud rainda replied to How to convert between TS f...
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).