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; } }