Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: XMI parser in java

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Location
    Chikmagalur
    Posts
    2
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default XMI parser in java

    I have written a parser in java for parsing a XMI file...I want the content i fetched to be stored in a hash table..Im working on it not getting how to store the Id and name in a hash table...please help

    XMI parser written is as follows

    import java.io.*;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    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;
     
        class States {
     
    	private String sxmiid;
     
    	private String sname;
     
    	public States() {
     
    	}
     
    	public States(String sxmiid,String sname) {
    		this.sxmiid = sxmiid;
    		this.sname = sname;
     
     
    	}
    	public String getSxmiid() {
    		return sxmiid;
    	}
     
    	public void setSxmiid(String sxmiid) {
    		this.sxmiid = sxmiid;
    	}
     
     
    	public String getSname() {
    		return sname;
    	}
     
    	public void setSname(String sname) {
    		this.sname = sname;
    	}
     
     
     
    	public String toString() {
    		StringBuffer sb = new StringBuffer();
    		sb.append("State id:" + getSxmiid());
    		sb.append(", ");
    		sb.append("State name:" + getSname());
    		sb.append(". ");
    		return sb.toString();
    	}
    }
     
    class Events {
     
    	private String exmiid;
     
    	private String ename;
     
    	private String esource;
     
    	private String etarget;
     
    	public Events(){
     
    	}
     
    	public Events(String exmiid,String ename,String esource,String etarget) {
     
    		this.exmiid = exmiid;
    		this.ename = ename;
    		this.esource = esource;
    		this.etarget = etarget;
     
    	}
     
    	public String getExmiid() {
    		return exmiid;
    	}
     
    	public void setExmiid(String exmiid) {
    		this.exmiid  = exmiid;
    	}
     
     
     
    	public String getEname() {
    		return ename;
    	}
     
    	public void setEname(String ename) {
    		this.ename = ename;
    	}	
     
     
    	public String getEsource() {
    		return esource;
    	}
     
    	public void setEsource(String esource) {
    		this.esource = esource;
    	}
     
    	public String getEtarget() {
     
    		return etarget;
    	}
     
    	public void setEtarget(String etarget) {
    		this.etarget = etarget;
    	}
     
     
    	public String toString() {
    		StringBuffer sb = new StringBuffer();
    		sb.append("Event id:" + getExmiid());
    		sb.append(", ");
    		sb.append("Event name:" + getEname());
    		sb.append(". ");
    		return sb.toString();
     
    	}
    }
     
     
    public class XMIParser extends DefaultHandler {
     
    	//List myEmpls;
    	List myStates;
     
    	List myEvents;
     
    	String file;
     
    	private String tempVal;
     
    	private States tempStates;
    	private Events tempEvents;
     
    	public XMIParser() {
    		myStates = new ArrayList();
    		myEvents = new ArrayList();
    	}
     
    	public void runParser() throws IOException {
    		parseDocument();
    		printData();
    		printFile();
    	}
     
    	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("cwm.xmi",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("STATES_BEGIN");
    		file="STATES_BEGIN\r\n\r\n";
    		Iterator its = myStates.iterator();
    		while(its.hasNext()) {
    			//System.out.println(its.next().toString());
    			file=file+its.next().toString()+"\r\n";
    		}
    		//System.out.println("STATES_END\n\n");
    		file=file+"\r\nSTATES_END\r\n\r\n\r\n";		
     
    		//System.out.println("TRANSITION_BEGIN");
    		file=file+"TRANSITION_BEGIN\r\n\r\n";
    		Iterator ite = myEvents.iterator();
    		while(ite.hasNext()) {
    			//System.out.println(ite.next().toString());
    			file=file+ite.next().toString()+"\r\n";
    		}
    		//System.out.println("TRANSITION_END\n\n");
    		file=file+"\r\nTRANSITION_END\r\n";
    	}
    	private void printFile() throws FileNotFoundException,IOException {
    		byte buff[]=file.getBytes();
    		OutputStream fo=new FileOutputStream("file2.txt");
    		for(int i=0;i<buff.length;i++)
    		fo.write(buff[i]);
    		fo.close();
    	}
     
     
    	//Event Handlers
    	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
     
    		tempVal = "";
    		if(qName.equalsIgnoreCase("subvertex")) {
     
    			tempStates = new States();
    			tempStates.setSxmiid(attributes.getValue("xmi:id"));
    			tempStates.setSname(attributes.getValue("name"));
    		}
    		else if(qName.equalsIgnoreCase("transition")) {
     
    			tempEvents = new Events();
    			tempEvents.setExmiid(attributes.getValue("xmi:id"));
    			tempEvents.setEname(attributes.getValue("name"));
    		}
    	}
     
     
    	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("subvertex")) {
    			myStates.add(tempStates);
     
    		}
    		else if (qName.equalsIgnoreCase("transition")) {
    			myEvents.add(tempEvents);
    		}
     
     
    	}
     
    	public static void main(String[] args)throws IOException{
    		XMIParser xp = new XMIParser();
    		xp.runParser();
    	}
     
    }
     
    /*end of XMI parser*/


    The XMI file parsed is shown below

    cwm.xmi
    <?xml version="1.0" encoding="UTF-8"?>
    <xmi:XMI xmi:version="2.1" xmlns:uml="http://schema.omg.org/spec/UML/2.1" xmlns:xmi="http://schema.omg.org/spec/XMI/2.1">
    <xmi:Documentation exporter="Bouml" exporterVersion="1.14.1"/>
    <uml:Model xmi:type="uml:Model" xmi:id="themodel" name="cwm">
    <packagedElement xmi:type="uml:Package" xmi:id="BOUML_0x81_22" name ="cwm">
    <packagedElement xmi:type="uml:StateMachine" xmi:id="BOUML_0x1f464_23" name="Coffee">
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f464_23" name="Bouml_Implicit_Region">
    <subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f4e4_23" name="Idle">
    <incoming xmi:idref="BOUML_0x1f864_24"/>
    <incoming xmi:idref="BOUML_0x22be4_24"/>
    <incoming xmi:idref="BOUML_0x1f464_24"/>
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f4e4_23" name="Bouml_Implicit_Region">
    <outgoing xmi:idref="BOUML_0x1f4e4_24"/>
    </region>
    </subvertex>
    <subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f564_23" name="Waiting">
    <incoming xmi:idref="BOUML_0x1f4e4_24"/>
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f564_23" name="Bouml_Implicit_Region">
    <outgoing xmi:idref="BOUML_0x1f564_24"/>
    </region>
    </subvertex>
    <subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f5e4_23" name="Verification">
    <incoming xmi:idref="BOUML_0x1f564_24"/>
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f5e4_23" name="Bouml_Implicit_Region">
    <outgoing xmi:idref="BOUML_0x1f5e4_24"/>
    <outgoing xmi:idref="BOUML_0x1f764_24"/>
    </region>
    </subvertex>
    <subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f664_23" name="Valve open">
    <incoming xmi:idref="BOUML_0x1f5e4_24"/>
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f664_23" name="Bouml_Implicit_Region">
    <outgoing xmi:idref="BOUML_0x1f664_24"/>
    </region>
    </subvertex>
    <subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f6e4_23" name="Valve close">
    <incoming xmi:idref="BOUML_0x1f664_24"/>
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f6e4_23" name="Bouml_Implicit_Region">
    <outgoing xmi:idref="BOUML_0x1f864_24"/>
    </region>
    </subvertex>
    <subvertex xmi:type="uml:State" xmi:id="BOUML_0x1f764_23" name="Error">
    <incoming xmi:idref="BOUML_0x1f764_24"/>
    <region xmi:type="uml:Region" xmi:id="BOUML_IMPLICIT_REGION_0x1f764_23" name="Bouml_Implicit_Region">
    <outgoing xmi:idref="BOUML_0x22be4_24"/>
    </region>
    </subvertex>
    <subvertex xmi:type="uml:Pseudostate" xmi:id="BOUML_0x1f464_28" kind="initial">
    <outgoing xmi:idref="BOUML_0x1f464_24"/>
    </subvertex>
    </region>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f4e4_24" name="Inserting 1st coin" source="BOUML_0x1f4e4_23" target="BOUML_0x1f564_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f564_24" name="Inserting 2nd coin" source="BOUML_0x1f564_23" target="BOUML_0x1f5e4_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f5e4_24" name="Signaling valve" source="BOUML_0x1f5e4_23" target="BOUML_0x1f664_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f764_24" name="Inserting wrong coin" source="BOUML_0x1f5e4_23" target="BOUML_0x1f764_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f664_24" name="Supply of coffee" source="BOUML_0x1f664_23" target="BOUML_0x1f6e4_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f864_24" name="System signal" source="BOUML_0x1f6e4_23" target="BOUML_0x1f4e4_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x22be4_24" name="reset signal" source="BOUML_0x1f764_23" target="BOUML_0x1f4e4_23">
    </transition>
    <transition xmi:type="uml:Transition" xmi:id="BOUML_0x1f464_24" name="process start" source="BOUML_0x1f464_28" target="BOUML_0x1f4e4_23">
    </transition>
    </packagedElement>
    </packagedElement>
    </uml:Model>
    </xmi:XMI>


    /*end of xmi file*/
    The content I fetched is stored in the file2.txt
    the file is shown below
    STATES_BEGIN

    State id:BOUML_0x1f4e4_23, State name:Idle.
    State id:BOUML_0x1f564_23, State name:Waiting.
    State id:BOUML_0x1f5e4_23, State name:Verification.
    State id:BOUML_0x1f664_23, State name:Valve open.
    State id:BOUML_0x1f6e4_23, State name:Valve close.
    State id:BOUML_0x1f764_23, State name:Error.
    State id:BOUML_0x1f464_28, State name:null.

    STATES_END


    TRANSITION_BEGIN

    Event id:BOUML_0x1f4e4_24, Event name:Inserting 1st coin.
    Event id:BOUML_0x1f564_24, Event name:Inserting 2nd coin.
    Event id:BOUML_0x1f5e4_24, Event name:Signaling valve.
    Event id:BOUML_0x1f764_24, Event name:Inserting wrong coin.
    Event id:BOUML_0x1f664_24, Event name:Supply of coffee.
    Event id:BOUML_0x1f864_24, Event name:System signal.
    Event id:BOUML_0x22be4_24, Event name:reset signal.
    Event id:BOUML_0x1f464_24, Event name:process start.

    TRANSITION_END
    /*end of file2.txt*/


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: XMI parser in java

    Hello chiru. Welcome to the Java Programming Forums.

    What happens when you run the code? Are any exceptions thrown?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Location
    Chikmagalur
    Posts
    2
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: XMI parser in java

    we r running on jdk 1.6, j creator,running successfully. But i want the content i fetched to be stored in a hash table,like i need the state id and state name in the hash table.if i give state id ,state name should be fetched from the hash table.

Similar Threads

  1. sql/jdbc parser
    By anand.stk in forum Member Introductions
    Replies: 1
    Last Post: January 27th, 2011, 09:43 AM
  2. Parser for java Class
    By duarte in forum Java Theory & Questions
    Replies: 1
    Last Post: November 16th, 2010, 07:22 PM
  3. Java Parser
    By sid0009 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 20th, 2010, 11:06 AM
  4. XML DOM Parser problem
    By kanishktew in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 09:42 PM
  5. where can i get a Dom Parser jar ?
    By chinni in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: November 26th, 2009, 03:41 AM