Xml in Modelica

model XMLExample
  import Modelica.Utilities.Streams;
  import Modelica.Utilities.Files;
  import Modelica.Utilities.System;

  record Plant
    String name;
    Integer id;
    String[:] origin;
  end Plant;

  function plantToString
    input Plant p;
    output String str;
  algorithm
    str := "Plant id=" + String(p.id) + ", name=" + p.name + ", origin=" + arrayToString(p.origin);
  end plantToString;

  function arrayToString
    input String[:] arr;
    output String str;
  algorithm
    str := "{" + sum(arr[i] + (if i < size(arr, 1) then ", " else "") for i in 1:size(arr, 1)) + "}";
  end arrayToString;

  function main
    Plant coffee;
    Plant tomato;
    String xml;
  algorithm
    coffee := Plant(name="Coffee", id=27, origin={"Ethiopia", "Brazil"});
    tomato := Plant(name="Tomato", id=81, origin={"Mexico", "California"});

    // Emitting XML
    xml := "<plant id=\"" + String(coffee.id) + "\">\n" +
           "  <name>" + coffee.name + "</name>\n" +
           "  <origin>" + coffee.origin[1] + "</origin>\n" +
           "  <origin>" + coffee.origin[2] + "</origin>\n" +
           "</plant>";
    
    Streams.print(xml);
    
    // Adding XML header
    Streams.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xml);
    
    // Parsing XML (simplified)
    Streams.print(plantToString(coffee));
    
    // Nesting XML
    xml := "<nesting>\n" +
           "  <parent>\n" +
           "    <child>\n" +
           "      <plant id=\"" + String(coffee.id) + "\">\n" +
           "        <name>" + coffee.name + "</name>\n" +
           "        <origin>" + coffee.origin[1] + "</origin>\n" +
           "        <origin>" + coffee.origin[2] + "</origin>\n" +
           "      </plant>\n" +
           "      <plant id=\"" + String(tomato.id) + "\">\n" +
           "        <name>" + tomato.name + "</name>\n" +
           "        <origin>" + tomato.origin[1] + "</origin>\n" +
           "        <origin>" + tomato.origin[2] + "</origin>\n" +
           "      </plant>\n" +
           "    </child>\n" +
           "  </parent>\n" +
           "</nesting>";
    
    Streams.print(xml);
  end main;

equation
  when initial() then
    main();
  end when;
end XMLExample;

This Modelica code demonstrates XML handling, although it’s important to note that Modelica doesn’t have built-in XML support like Go does. Instead, we’ve simulated XML creation and manipulation using string operations.

Here’s a breakdown of the code:

  1. We define a Plant record to represent our data structure.

  2. The plantToString and arrayToString functions are helper functions for string representation of our data.

  3. In the main function:

    • We create Plant instances for coffee and tomato.
    • We manually construct XML strings representing these plants.
    • We print the XML strings, including with an XML header.
    • We demonstrate a simple “parsing” by printing the string representation of the coffee plant.
    • Finally, we create a nested XML structure with both plants.
  4. The when initial() equation ensures that the main function is called when the model is initialized.

Note that this is a simplified representation. Modelica doesn’t have native XML parsing capabilities, so we’ve focused on XML generation. In a real-world scenario, you might use external C functions or libraries for more robust XML handling in Modelica.

To run this example, you would need to use a Modelica simulation environment that supports the Modelica Standard Library, particularly the Modelica.Utilities package for string and file operations.