The source code of the Stenway SML reference library can be found on GitHub
Empty root element:
SmlElement rootElement = new SmlElement("MyRootElement"); SmlDocument document = new SmlDocument(rootElement);
Serialize an SML document:
SmlElement rootElement = new SmlElement("MyRootElement"); SmlDocument document = new SmlDocument(rootElement); String str = document.toString();
Message example:
SmlElement message = new SmlElement("Message"); message.addAttribute("From", "Andrew"); message.addAttribute("To", "James"); message.addAttribute("Timestamp", "2021-01-02", "15:43"); message.addAttribute("Text", "I'll be there at 5pm"); SmlDocument document = new SmlDocument(message);
Save as SML file:
SmlElement message = new SmlElement("Message"); message.addAttribute("From", "Andrew"); message.addAttribute("To", "James"); message.addAttribute("Timestamp", "2021-01-02", "15:43"); message.addAttribute("Text", "I'll be there at 5pm"); SmlDocument document = new SmlDocument(message); document.save("Message1.sml");
Load an SML file:
SmlDocument document = SmlDocument.load("Message1.sml");
Get value:
SmlDocument document = SmlDocument.load("Message1.sml"); String from = document.getRoot().attribute("From").getString();
Load an SML file as WSV document:
WsvDocument wsvDocument = WsvDocument.load("Message1.sml"); String wsvStr = wsvDocument.toString(); SmlDocument smlDocument = SmlDocument.parse(wsvStr);
Load an SML file and change it:
SmlDocument document = SmlDocument.load("Config.sml"); SmlElement videoElement = document.getRoot().element("Video"); SmlAttribute resolutionAttribute = videoElement.attribute("Resolution"); resolutionAttribute.setValues(1920, 1080); resolutionAttribute.setComment(" Changed"); if (!videoElement.hasAttribute("VSync")) { videoElement.addAttribute("VSync", true); }
Save a localized file:
SmlElement message = new SmlElement("Message"); message.addAttribute("De", "André"); message.addAttribute("À", "Jacques"); message.addAttribute("Horodatage", "2021-01-02", "15:43"); message.addAttribute("Texte", "Je serai là à 5 heures"); SmlDocument document = new SmlDocument(message); document.setEndKeyword("Fin"); document.save("Français.sml");
Load a localized SML file:
SmlDocument document = SmlDocument.load("Français.sml");
Naming:
SmlDocument document = SmlDocument.load("FileList.sml"); SmlAttribute[] images = document.getRoot().attributes("Image"); SmlAttribute[] videos = document.getRoot().attributes("Video"); for (SmlAttribute image : images) { String imageFilePath = image.getString(); ... }
Null value and empty string:
SmlElement rootElement = new SmlElement("MyRootElement"); rootElement.addAttribute("MyFirstAttribute", "-", null, "");
Multi-line string:
SmlElement rootElement = new SmlElement("MyRootElement"); rootElement.addAttribute("MyFirstAttribute", "Line1\nLine2\nLine3"); rootElement.addAttribute("MySecondAttribute", """ Line1 Line2 Line3"""); rootElement.addAttribute("MyThirdAttribute", ReliableTxtDocument.join("Line1", "Line2", "Line3"));
Indentation-insensitive:
SmlElement rootElement = new SmlElement("MyRootElement"); SmlElement group1Element = rootElement.addElement("Group1"); group1Element.addAttribute("MyFirstAttribute", 123); group1Element.addAttribute("MySecondAttribute", 10, 20, 30, 40, 50); rootElement.addAttribute("MyThirdAttribute", "Hello world"); SmlDocument document = new SmlDocument(rootElement); document.setDefaultIndentation("");
Read an SML file node by node:
try (SmlStreamReader reader = new SmlStreamReader("Big.sml")) { SmlNode node = null; while ((node = reader.readNode()) != null) { ... } }
Write an SML file node by node:
SmlDocument template = new SmlDocument("MyRootElement"); try (SmlStreamWriter writer = new SmlStreamWriter(template, "Big.sml", ReliableTxtEncoding.UTF_16, true)) { writer.writeNode(new SmlElement("MyElement")); writer.writeNode(new SmlAttribute("MyAttribute", "Value")); }