Problems Parsing this file.

I am tring to parse sections of this file. I am not having any luck I do get the TagName, but that is it. What am I doing wrong?

Here is the xml file: <?xml version="1.0" encoding="UTF-8"?>

<TSConfig _CfgName="test" _CfgDescription="Test CLX Configuration" _ScnrVer="0.19">

<DrvInst _DrvLib="CLXBP" _DrvLibVer="0.00" _DrvName="Drv1" ModuleFamily="2" Path="p:1,s:0" RspToMs="5000">

<StructDef _StructName="TIMER" _ExtType="F83" _EleSize="40">

<StructMbr _MbrName="CTL" _MbrDaOfs="0" _BaseType="BYTE_BOOL" _ExtType="C1" _EleSize="1" _xDim="32" />

<StructMbr _MbrName="PRE" _MbrDaOfs="32" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

<StructMbr _MbrName="ACC" _MbrDaOfs="36" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

</StructDef>

<TagInst _TagName="Test_INT" _BaseType="INT16" _ExtType="C3" _EleSize="2" />

<TagInst _TagName="Test_DINT" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

<TagInst _TagName="autoFillTag" _BaseType="INT16" _ExtType="C3" _EleSize="2" />

<TagInst _TagName="AutoFillTest_2" _BaseType="INT32" _ExtType="C4" _EleSize="4" />

<TagInst _TagName="Test_REAL" _BaseType="FLOAT32" _ExtType="CA" _EleSize="4" />

<TagInst _TagName="Test_SINT" _BaseType="INT8" _ExtType="C2" _EleSize="1" />

<TagInst _TagName="MVI_RecvMsgData" _BaseType="INT8" _ExtType="C2" _EleSize="1" _xDim="500" />

<TagInst _TagName="Timer1" _BaseType="STRUCT" _ExtType="F83" _EleSize="40" />

</DrvInst>

<DrvInst _DrvLib="CLXBP" _DrvLibVer="0.00" _DrvName="Logix5563" ModuleFamily="2" Path="p:1,s:5" RspToMs="5000">

<TagInst _TagName="dummy1" _BaseType="INT16" _ExtType="C3" _EleSize="2" />

</DrvInst>

<Trigger _TrigName="Trig1" _Type="Poll" _Period="1000" _Variable="1" _Condition="NEQ" _Reference="0">

<Action _TLName="RunEvery1000mS" />

<Action _TLName="RunEvery100mS" />

</Trigger>

<TransferList _TLName="RunEvery1000mS">

<Transfer _XferId="1" _Dest="Drv1\Test_INT" _Source="1234" />

<Transfer _XferId="2" _Dest="Drv1\Test_DINT" _Source="12345678" />

<Transfer _XferId="3" _Dest="Drv1\Test_DINT" _Source="Drv1\Test_INT" />

<Transfer _XferId="4" _Source="56" _Dest="Drv1\Test_SINT" />

</TransferList>

<Trigger _TrigName="Trig2" _Type="Poll" _Period="500" _Variable="1" _Condition="NEQ" _Reference="0">

<Action _TLName="RunEvery500mS" />

</Trigger>

<TransferList _TLName="RunEvery500mS" />

<TransferList _TLName="RunEvery5000mS" />

<TransferList _TLName="RunEvery100mS" />

<TransferList _TLName="RunEvery200mS" />

<Trigger _TrigName="Trig3" _Type="Poll" _Period="5000" _Variable="1" _Condition="NEQ" _Reference="0">

<Action _TLName="RunEvery200mS" />

</Trigger>

<DrvInst _DrvLib="CLXBP" _DrvLibVer="0.00" _DrvName="ENET" ModuleFamily="1" Path="p:1,s:2" RspToMs="5000">

<DevInst _DevName="PLC5_40E" ModuleFamily="3" Path="p:1,s:2,p:2,t:10.0.104.105" RspToMs="5000">

<TagInst _TagName="n7_0" _BaseType="INT16" RegName="N7:0" xDim="0" _EleSize="2" />

</DevInst>

</DrvInst>

</TSConfig>

Here is the Parser that I am using:

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

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;

publicclass SAXParserExampleextends DefaultHandler

{

List myTemplate;

private String tempVal;

//to maintain context

private Template tmpTemplate;

public SAXParserExample()

{

myTemplate =new ArrayList();

}

publicvoid runExample()

{

parseDocument();

printData();

}

privatevoid parseDocument()

{

//get a factory

SAXParserFactory spf = SAXParserFactory.newInstance();

try

{

//get a new instance of parser

SAXParser sp = spf.newSAXParser();

//parse the file and also register this class for call backs

sp.parse("XML/test.xml",this);

}

catch(SAXException se)

{

se.printStackTrace();

}

catch(ParserConfigurationException pce)

{

pce.printStackTrace();

}

catch (IOException ie)

{

ie.printStackTrace();

}

}

/**

* Iterate through the list and print

* the contents

*/

privatevoid printData()

{

System.out.println("No of Tags '" + myTemplate.size() +"'.");

Iterator it = myTemplate.iterator();

while(it.hasNext())

{

System.out.println(it.next().toString());

}

}

//Event Handlers

publicvoid startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException

{

//reset

tempVal ="";

if(qName.equalsIgnoreCase("TagInst"))

{

//create a new instance of employee

tmpTemplate =new Template();

tmpTemplate.setName(attributes.getValue("_TagName"));

}

}

publicvoid characters(char[] ch,int start,int length)throws SAXException

{

tempVal =new String(ch,start,length);

}

publicvoid endElement(String uri, String localName, String qName)throws SAXException

{

if(qName.equalsIgnoreCase("TagInst"))

{

//add it to the list

myTemplate.add(tmpTemplate);

}

elseif (qName.equalsIgnoreCase("_BaseType"))

{

tmpTemplate.setBaseType(tempVal);

}

elseif (qName.equalsIgnoreCase("_ExtType"))

{

tmpTemplate.setExtType(tempVal);

}

elseif (qName.equalsIgnoreCase("_EleSize"))

{

tmpTemplate.setEleSize(tempVal);

}

}

publicstaticvoid main(String[] args)

{

SAXParserExample spe =new SAXParserExample();

spe.runExample();

}

}

and here is the Getter and Setter class that goes along with it.

publicclass Template

{

private String name;

private String baseType;

private String extType;

private String eleSize;

public Template()

{

}

public Template(String name, String baseType, String extType ,String eleSize)

{

this.name = name;

this.baseType = baseType;

this.extType = extType;

this.eleSize = eleSize;

}

public String getName()

{

return name;

}

publicvoid setName(String name)

{

this.name = name;

}

public String getBaseType()

{

return baseType;

}

publicvoid setBaseType(String baseType)

{

this.baseType = baseType;

}

public String getExtType()

{

return extType;

}

publicvoid setExtType(String extType)

{

this.extType = extType;

}

public String getEleSize()

{

return eleSize;

}

publicvoid setEleSize(String eleSize)

{

this.eleSize = eleSize;

}

public String toString()

{

StringBuffer sb =new StringBuffer();

sb.append("Tag Details - ");

sb.append("Name: " + getName());

sb.append(", ");

sb.append("Base Type: " + getBaseType());

sb.append(", ");

sb.append("Ext Type: " + getExtType());

sb.append(", ");

sb.append("Element Size: " + getEleSize());

sb.append(".");

return sb.toString();

}

}

and this is the Data that I get:

No of Tags'10'.

Tag Details - Name: Test_INT, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: Test_DINT, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: autoFillTag, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: AutoFillTest_2, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: Test_REAL, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: Test_SINT, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: MVI_RecvMsgData, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: Timer1, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: dummy1, Base Type: null, Ext Type: null, Element Size: null.

Tag Details - Name: n7_0, Base Type: null, Ext Type: null, Element Size: null.

Everything after the name is null? Plus I don't really want the last two tags. I am just trying get sections of this XML.

[17370 byte] By [orozcoma] at [2007-11-26 13:51:55]
# 1
Just a guess but the tags like '_BaseType' are attributes not elements.
jschella at 2007-7-8 1:29:24 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

No, this is a home grown xml file for our product. This file is actually fed to a small embedded device that is read using c code. My job is just to be able make a xml editor (if you will), but make it easier to modify the elements. I have a program where I can parse this xml file and create it into a JTree. I can then click on each individual node of the JTree and display that nodes information in a JPanel. The problem is getting the elements through parsing to the panel. This is why I am trying to do sections.

I took this information from another file. Here they are.

public class Template

{

private String name;

private int age;

private int id;

private String type;

public Template()

{

}

public Template(String name, int id, int age,String type)

{

this.name = name;

this.age = age;

this.id = id;

this.type = type;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public int getId()

{

return id;

}

public void setId(int id)

{

this.id = id;

}

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public String getType()

{

return type;

}

public void setType(String type)

{

this.type = type;

}

public String toString()

{

StringBuffer sb = new StringBuffer();

sb.append("Employee Details - ");

sb.append("Name:" + getName());

sb.append(", ");

sb.append("Type:" + getType());

sb.append(", ");

sb.append("Id:" + getId());

sb.append(", ");

sb.append("Age:" + getAge());

sb.append(".");

return sb.toString();

}

}

And the xml file:

<Personnel>

<Employee type="permanent">

<Name>Seagull</Name>

<Id>3674</Id>

<Age>34</Age>

</Employee>

<Employee type="contract">

<Name>Robin</Name>

<Id>3675</Id>

<Age>25</Age>

</Employee>

<Employee type="permanent">

<Name>Crow</Name>

<Id>3676</Id>

<Age>28</Age>

</Employee>

</Personnel>

The only thing that needs to be changed is the name of the xml file in the SaxParserExample class.

orozcoma at 2007-7-8 1:29:24 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

Oops I was wrong. You will need this SaxParserExample class

import java.io.IOException;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

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;

import misc.*;

public class SAXParserExample extends DefaultHandler

{

List myTemplate;

private String tempVal;

//to maintain context

private Template tmpTemplate;

public SAXParserExample()

{

myTemplate = new ArrayList();

}

public void runExample()

{

parseDocument();

printData();

}

private void parseDocument()

{

//get a factory

SAXParserFactory spf = SAXParserFactory.newInstance();

try

{

//get a new instance of parser

SAXParser sp = spf.newSAXParser();

//parse the file and also register this class for call backs

sp.parse("XML/employees.xml", this);

}

catch(SAXException se)

{

se.printStackTrace();

}

catch(ParserConfigurationException pce)

{

pce.printStackTrace();

}

catch (IOException ie)

{

ie.printStackTrace();

}

}

/**

* Iterate through the list and print

* the contents

*/

private void printData()

{

System.out.println("No of Employees '" + myTemplate.size() + "'.");

Iterator it = myTemplate.iterator();

while(it.hasNext())

{

System.out.println(it.next().toString());

}

}

//Event Handlers

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

{

//reset

tempVal = "";

if(qName.equalsIgnoreCase("Employee"))

{

//create a new instance of employee

tmpTemplate = new Template();

tmpTemplate.setType(attributes.getValue("type"));

}

}

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

{

tempVal = new String(ch,start,length);

}

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

{

if(qName.equalsIgnoreCase("Employee"))

{

//add it to the list

myTemplate.add(tmpTemplate);

}

else if (qName.equalsIgnoreCase("Name"))

{

tmpTemplate.setName(tempVal);

}

else if (qName.equalsIgnoreCase("Id"))

{

tmpTemplate.setId(Integer.parseInt(tempVal));

}

else if (qName.equalsIgnoreCase("Age"))

{

tmpTemplate.setAge(Integer.parseInt(tempVal));

}

}

public static void main(String[] args)

{

SAXParserExample spe = new SAXParserExample();

spe.runExample();

}

}

also, I have a folder XML at the root of my project. You can change this sp.parse("XML/employees.xml", this);to where ever you put this xml file.

orozcoma at 2007-7-8 1:29:24 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4

It sounds like you're suggesting we should try running your code on our computers or something like that. Well forget that. I already know the answer and it's what jschell told you. Those things you are looking for are attributes and you're looking for them as if they were elements.

Hint: the startElement() method gives you an Attributes object. Use it.

DrClapa at 2007-7-8 1:29:24 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Thanks for the help. I figured it out. I was setting the attribute for the

tmpTemplate.setName(attributes.getValue("_TagName"));

but that was all. After I added each attribute setting.

tmpTemplate.setBaseType(attributes.getValue("_BaseType"));

tmpTemplate.setExtType(attributes.getValue("_ExtType"));

tmpTemplate.setEleSize(attributes.getValue("_EleSize"));

I got the information I needed.

No of Tags '10'.

Tag Details - Name: Test_INT, Base Type: INT16, Ext Type: C3, Element Size: 2.

Tag Details - Name: Test_DINT, Base Type: INT32, Ext Type: C4, Element Size: 4.

Tag Details - Name: autoFillTag, Base Type: INT16, Ext Type: C3, Element Size: 2.

Tag Details - Name: AutoFillTest_2, Base Type: INT32, Ext Type: C4, Element Size: 4.

Tag Details - Name: Test_REAL, Base Type: FLOAT32, Ext Type: CA, Element Size: 4.

Tag Details - Name: Test_SINT, Base Type: INT8, Ext Type: C2, Element Size: 1.

Tag Details - Name: MVI_RecvMsgData, Base Type: INT8, Ext Type: C2, Element Size: 1.

Tag Details - Name: Timer1, Base Type: STRUCT, Ext Type: F83, Element Size: 40.

Tag Details - Name: dummy1, Base Type: INT16, Ext Type: C3, Element Size: 2.

Tag Details - Name: n7_0, Base Type: INT16, Ext Type: null, Element Size: 2.

Now I am still getting two of the tagNames that I don't need. the dummy1, and n7_0 they are on different driver Instruc than the one's I need. Of course this app runs through the whole xml file and matches the attributes that are given.

Any help would be appreciated.

orozcom

orozcoma at 2007-7-8 1:29:24 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

> Now I am still getting two of the tagNames that I

> don't need. the dummy1, and n7_0 they are on

> different driver Instruc than the one's I need. Of

> course this app runs through the whole xml file and

> matches the attributes that are given.

I am guessing that you mean that the hierarchy is different.

You will have to maintain the hierarchy yourself to eliminate that.

jschella at 2007-7-8 1:29:24 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...