Reading a closing XML tag

Joined
May 25, 2009
Messages
2
Reaction score
0
Need to modify my program to read a xml files that closes tags with />. For example my program only works with the following tags:

<?xml version="1.0"?>
<root gmtdate="05-26-2009" gmttime="17:24:33" totalEvents="391">
<event id="1" gmttimecode="1243306566" gmtdate="05-26-2009" gmttime="02:56:06" type="Added" category="DPI" msg="Heartbeat Lost"></event>
<event id="2" gmttimecode="1243307166" gmtdate="05-26-2009" gmttime="03:06:06" type="Added" category="DPI" msg="Heartbeat Lost"></event>
</root>

I need it to be able to read the following tag:

<?xml version="1.0"?>
<root gmtdate="05-26-2009" gmttime="17:24:33" totalEvents="391">
<event id="1" gmttimecode="1243306566" gmtdate="05-26-2009" gmttime="02:56:06" type="Added" category="DPI" msg="Heartbeat Lost"/>
<event id="2" gmttimecode="1243307166" gmtdate="05-26-2009" gmttime="03:06:06" type="Added" category="DPI" msg="Heartbeat Lost"/>
</root>

As you can see the difference is the way the tag closes.

=================================================
event.java
=================================================
public class event {

private String id;
private String gmttimecode;
private String gmtdate;
private String gmttime;
private String type;
private String category;
private String msg;

public event(){

}

public event(String id, String gmttimecode, String gmtdate, String gmttime, String type, String category, String msg) {
this.id = id;
this.gmttimecode = gmttimecode;
this.gmtdate = gmtdate;
this.gmttime = gmttime;
this.type = type;
this.category = category;
this.msg = msg;
}

public String getType() {
return id;
}

public void setType(String id) {
this.id = id;
}

public String getType2() {
return gmttimecode;
}

public void setType2(String gmttimecode) {
this.gmttimecode = gmttimecode;
}

public String getType3() {
return gmtdate;
}

public void setType3(String gmtdate) {
this.gmtdate = gmtdate;
}

public String getType4() {
return gmttime;
}

public void setType4(String gmttime) {
this.gmttime = gmttime;
}

public String getType5() {
return type;
}

public void setType5(String type) {
this.type = type;
}

public String getType6() {
return category;
}

public void setType6(String category) {
this.category = category;
}
public String getType7() {
return msg;
}

public void setType7(String msg) {
this.msg = msg;
}

public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("event ");
sb.append("id:" + getType());
sb.append(", ");
sb.append("gmttimecode:" + getType2());
sb.append(", ");
sb.append("gmtdate:" + getType3());
sb.append(", ");
sb.append("gmttime:" + getType4());
sb.append(", ");
sb.append("type:" + getType5());
sb.append(", ");
sb.append("category:" + getType6());
sb.append(", ");
sb.append("msg:" + getType7());
sb.append(", ");
return sb.toString();
}
}
=======================================================
SencoreLogParser.java
=======================================================
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class SencoreLogParser {
List myEmpls;
Document dom;

public SencoreLogParser(){
myEmpls = new ArrayList();
}

public void runExample() {
parseXmlFile();
parseDocument();
printData();
}

private void parseXmlFile(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {

DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse("logDay0.xml");
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}

private void parseDocument(){
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName("event");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {
Element el = (Element)nl.item(i);
event e = getEmployee(el);
myEmpls.add(e);
}
}
}

private event getEmployee(Element empEl) {
String id = empEl.getAttribute("id");
String gmttimecode = empEl.getAttribute("gmttimecode");
String gmtdate = empEl.getAttribute("gmtdate");
String gmttime = empEl.getAttribute("gmttime");
String type = empEl.getAttribute("type");
String category = empEl.getAttribute("category");
String msg = empEl.getAttribute("msg");
event e = new event(id,gmttimecode,gmtdate,gmttime,type,category,msg);
return e;
}

private void printData(){
System.out.println("No of SCTE 104 Events: '" + myEmpls.size() + "'.");
Iterator it = myEmpls.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}

public static void main(String[] args){
SencoreLogParser dpe = new SencoreLogParser();
dpe.runExample();
}

}

========================================================
Program Output
========================================================
No of SCTE 104 Events: '2'.
event id:1, gmttimecode:1243306566, gmtdate:05-26-2009, gmttime:02:56:06, type:Added, category:DPI, msg:Heartbeat Lost,
event id:2, gmttimecode:1243307166, gmtdate:05-26-2009, gmttime:03:06:06, type:Added, category:DPI, msg:Heartbeat Lost,
========================================================
Any help is appreciated. Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top