Search code examples
javajsoupmusicxml

Iterating over different elements and changing a particular one in JSoup


I need to change a few elements, which are nested deep within a musicxml file. I use jSoup to parse the document and perform my calculations.

Now I want to expert the jsoup doc and make a few modifications first. The problem is, within the xml file, the elements don't have a unique identifier (e.g. there are many notes and measures, notes are not numbered).

I loop through the document like this. And after certain criteria are met, I want to change the particular note. Using this iter in java uses a copy, so modifying the elements doesn't make a difference on the original doc. Could I use for i = 0; i < ?? or something? Would this go over the elements in the same order (important for checking criteria).

for (Element thismeasure : thisPart.getElementsByTag("measure")) {

                for (Element thisnote : thismeasure.children()) {

Solution

  • I used a more "traditional" method:

    for (int z = 0; z < this.doc.select("part").size(); z++ ){
                for (int y = 0; y <  this.doc.select("part").get(z).getElementsByTag("measure").size(); y++){
    ...
    

    This allows me to use the set method to change the elements in the actual doc variable.