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 1 of 1

Thread: How to Parse MusicXML file using DOM parser?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to Parse MusicXML file using DOM parser?

    Hi everyone, I am trying to parse musicXML file using DOM. However I am getting some error which I cant figure out why. Can someone help me to find this issue I am having and also can someone check if my coding is correct in terms or XML tree structure. The following code I have written sofar. I have attached the xml file that I am trying to work on.

    package de.uos.fmt.musitech.score.musicxml;
     
    import java.io.IOException;
    import java.net.URL;
     
    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.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
     
    import de.uos.fmt.musitech.data.score.NotationStaff;
    import de.uos.fmt.musitech.data.score.NotationSystem;
    import de.uos.fmt.musitech.data.score.NotationVoice;
    import de.uos.fmt.musitech.data.score.ScoreNote;
    import de.uos.fmt.musitech.data.structure.Context;
    import de.uos.fmt.musitech.data.structure.Note;
    import de.uos.fmt.musitech.data.structure.Piece;
    import de.uos.fmt.musitech.data.structure.linear.Part;
    import de.uos.fmt.musitech.score.mpegsmr.Attributes;
    import de.uos.fmt.musitech.utility.math.Rational;
     
    public class DomParser<Partwise> {
    	private Piece piece;
     
    	public DomParser(URL url){
    		parseXmlFile(url);	
    	}		
     
    	private void parseXmlFile(URL url){
    		//get the factory	
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		try {
    			//Using factory get an instance of document builder
    			DocumentBuilder db = dbf.newDocumentBuilder();
    			//parse using builder to get DOM representation of the XML file
    			Document dom = db.parse(url.openStream());
    			//Document dom = db.parse("music.xml");
    			piece = parseDocument(dom);
    		}catch(ParserConfigurationException pce) {
    			pce.printStackTrace();
    		}catch(SAXException se) {
    			se.printStackTrace();
    		}catch(IOException ioe) {
    			ioe.printStackTrace();
    		}		
    	}
    	private Piece parseDocument(Document dom){
    		//get the root element
    		Element docEle =  dom.getDocumentElement();
    		// create Musitech piece
    		Piece piece = new Piece();
     
    		// TODO distinction between partwise and measurewise/timewise (?)	
    		//partwise: get a nodelist of elements = parts
    		NodeList nl = docEle.getChildNodes();
    		if(nl != null && nl.getLength() > 0) {
    			for(int i = 0 ; i < nl.getLength();i++) {
    				Node node = nl.item(i);
    				if(node instanceof Element){
    					Element el = (Element)node;					
    					// switch  parts-list vs parts
    					if( el.getNodeName().equals("part")){
    						//create Musitech staff & voice
    						NotationStaff staff = new NotationStaff();
    						NotationVoice voice = new NotationVoice();
    						// go through notes in part
    						 NodeList cn = el.getChildNodes();
    						 for (int j = 0; j < cn.getLength(); j++) {
    							 Node n1 = cn.item(j);
    							 n1.getFirstChild();
    							 n1.getNextSibling();
    							 n1.getLastChild();
    							 staff.add(voice);
    //-------------------------------------------------------------------------
    							 if( el.getNodeName().equals("measure")){						
    								 NodeList cn1 = el.getChildNodes();			
    								 for (int k = 0; k < cn1.getLength(); k++) {
    									 Node n2 = cn1.item(k);	
    									 n2.getFirstChild();
    									 n2.getNextSibling();
    									 n2.getLastChild();
    									 //System.out.println(cn1);
     
    									 //attributes within measure
    									 if(el.getNodeName().equals("attributes")){
    										NodeList cn2 = el.getChildNodes();									
    										for (int s = 0; s <cn2.getLength(); s++){
    											Node n3 = cn2.item(s);
    											n3.getFirstChild();
    											n3.getNextSibling();
    											n3.getLastChild();
    											//if(n3.hasChildNodes()){
    												//Node n3 = getNote()
    											//}
    											//division within attributes
    											if(el.getNodeName().equals("division")){
    												el.getNodeValue();
    											}
    											//key within attributes
    											if(el.getNodeName().equals("key")){
    												NodeList cn3 = el.getChildNodes();
    												for (int m = 0; m <cn3.getLength(); m++){
    													Node n4 = cn3.item(m);
    													n4.getFirstChild();
    													if(n4.hasChildNodes()){
    														n4.getChildNodes();
    														n4.getNodeValue();
    													}
    													//if(el.getNodeName().equals("fifths")){
    														//Node n5 = n5.getNodeValue();
    													//}	
    												}
    											}				
    										}
    										//time within attributes
    										if(el.getNodeName().equals("time")){
    										 NodeList cn4 = el.getChildNodes();
    										 for (int n =0; n<cn4.getLength(); n++){
    											 Node n5 = cn4.item(n);
    											 if(n5.hasChildNodes()){
    												 n5.getChildNodes();
    												 n5.getNextSibling().getNodeValue(); 
    											 }
     
    										 }
    										}
    										//clef within attributes
    										if(el.getNodeName().equals("clef")){
    											 NodeList cn5 = el.getChildNodes();
    											 for (int o =0; o <cn5.getLength(); o++){
    												 Node n6 = cn5.item(o);
    												 if(n6.hasChildNodes()){
    													 n6.getChildNodes();
    													 n6.getNextSibling().getNodeValue();	
    												 }
    									 }											 
    						 		}
    										//note within measure 
    										if(el.getNodeName().equals("note")){
    											NodeList cn6 = el.getChildNodes();
    											for(int p = 0; p <cn6.getLength(); p++){
    												Node n7 = cn6.item(p);
    												n7.getFirstChild().getNextSibling().getNodeValue();
    												n7.getLastChild().getNextSibling().getNodeValue();
    												//pitch within note
    												if(el.getNodeName().equals("pitch")){
    													NodeList cn7 = el.getChildNodes();
    													for(int q =0; q <cn7.getLength(); q++){
    														Node n8 = cn6.item(q);
    														if(el.getNodeName().equals("step")){
    															el.getNodeValue();
    														}
    														if(el.getNodeName().equals("octave")){
    															el.getNodeValue();
    														}
    														n8.getFirstChild().getNextSibling().getNodeValue();												
    													}
    												}
    												if(el.getNodeName().equals("duration")){
    													el.getNodeValue();
    												}
    												if(el.getNodeName().equals("type")){
    													el.getNodeValue();
    												}
    											}
    										}
    									 }
    								 }
    							 }
     
    					}					
    				}
    			}
    		}
    	}
    	return piece;
    }
    	public Piece getPiece(){
    		return piece;
    	}
     
    	public Note getNote(Element el) {
    		// TODO Auto-generated method stub
    		ScoreNote snote = new ScoreNote();
    		//Note duration = getValue(el, "Duration");
    		//Note = type = getValue(el, "Type");
    		//getValue(el,"Duration");
    		//getValue(el,"Step");
    		snote.setDiatonic('C');
    		snote.setAlteration((byte)1);
    		snote.setMetricDuration(new Rational(1,4));
    		//snote.setOctave(0);
     
    		//Note  = (Note) ((DocumentBuilderFactory) el).getAttribute("note");		
    		//Create a new song with the value read from the xml nodes
    		//music = new Music (pitch,durution,time,key);
    		Note note = new Note(snote, null);
    		return note;
    	}
     
    	public Attributes getAttributes(Element el, Attributes tagName) {
    		Attributes attributes = null;
    		NodeList nl = (NodeList)el.appendChild(el);
    		if(nl != null && nl.getLength() > 0) {
    			Element e = (Element)nl.item(0);
    			//attributes = el.getNodeValue();
    		}
     
    		return attributes;
    	}
    	/**
    	 * Calls getTextValue and returns a int value
    	 */
    	public int getIntValue(Element ele, String tagName) {
    		//in production application you would catch the exception
    		return 0;
    	}	
    }
    Attached Files Attached Files

  2. The Following User Says Thank You to htechpro For This Useful Post:

    pierre.castellotti (January 28th, 2014)


Similar Threads

  1. How to parse an XML having same tag.
    By 1bun100 in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: February 24th, 2012, 08:34 AM
  2. How do I parse an input file in two parts?
    By bryanboy in forum Java Theory & Questions
    Replies: 4
    Last Post: November 12th, 2011, 02:20 AM
  3. HOW TO PARSE A TEXT FILE
    By hervebags in forum Algorithms & Recursion
    Replies: 4
    Last Post: June 1st, 2011, 09:36 AM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. [SOLVED] parse txt file
    By maliv in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: November 17th, 2010, 03:54 PM

Tags for this Thread