XML Functions

edited November 2013 in Programming Questions

I am creating a reference application for music theory that will compare frequencies from a piano roll to those on a guitar fretboard. I have two XML file schemas below, the second one modeled after Processing documentation. Is it possible to create a method in which I can pass in a string value and have it return the additional info for that node... I have tried all of the get methods for XML and am about to scrap it, but maybe I am missing something...

<?xml version="1.0"?>
<musicalNotes>
    <noteData>
        <noteName>C0</noteName>
        <noteFreq>16.35</noteFreq>
        <noteWave>2100</noteWave>
    </noteData>
    <noteData>
        <noteName>C#0</noteName>
        <noteFreq>17.32</noteFreq>
        <noteWave>1990</noteWave>
    </noteData>
</musicalNotes>

<?xml version="1.0"?>
<musicalNotes>
    <noteName id="0" noteFreq="16.35" noteWave="2100">C0</noteName>
    <noteName id="1" noteFreq="17.32" noteWave="1990">C#0</noteName>
</musicalNotes>

XML xml;
xml = loadXML("noteFreq.xml");

String matchInfo(String value) {
  // match the string value of note clicked to xml data
  XML noteInfo = xml.getChild(value);
  value = noteInfo.getContent(value);
  println(value);
  return value;
}

Answers

  • Answer ✓

    To retrieve attributes, it should be something like xml.getInt(). There are functions for retrieving each of the basic types. Of course, you can always get every value as a String and parse it later.

    String matchInfo(String value) {
      // match the string value of note clicked to xml data
      XML noteInfo = xml.getChild(value);
    
      String name = noteInfo.getContent(value);
      int id = noteInfo.getInt("id");
      float noteFreq = noteInfo.getFloat(noteFreq); //you might want more precision
      int noteWave = noteInfo.getInt(noteWave);
    }
    

    You can only return one of these values unless you create a wrapper class for the XML data (in some cases, XML itself is enough).

Sign In or Register to comment.