Nasal library/io: Difference between revisions

Line 366: Line 366:
{{Nasal doc
{{Nasal doc
|syntax = io.write_properties(path, prop);
|syntax = io.write_properties(path, prop);
|text = Writes nodes from the [[Property Tree]] to an XML file in FlightGear's [[PropertyList XML files|PropertyList]] format. Returns the filename on success or <code>'''nil'''</code> on error.
|text = Writes nodes from the [[Property Tree]] to an XML file in FlightGear's [[PropertyList XML files|PropertyList]] format. If the file does not exist, it will be created. If it does, all contents will be overwritten. Returns the filename on success or <code>'''nil'''</code> on error.
|param1 = path
|param1 = path
|param1text = Path of the file to write to.  
|param1text = Path of the file to write to. If it does not have a <tt>.xml</tt> extension, <tt>.xml</tt> will be added to it.
|param1 = prop
|param2 = prop
|param1text = Either a property path or a <code>props.Node</code> object. Note that, when the latter is used, results will be more accurate if it refers to a node in the Property Tree, otherwise the node will have to be copied, which may change the node type.
|param2text = Either a property path or a <code>props.Node</code> object. Note that, when the latter is used, results will be more accurate if it refers to a node in the Property Tree, otherwise the node will have to be copied, which may change the node type.
|example1 = var data = props.Node.new({ a:1, b:2, c:{ d:3, e:4 } });  
|example1 = var path = getprop("/sim/fg-home") ~ '/Export/demo.xml';
io.write_properties("/tmp/foo.xml", data);              
var prop = "/position";
io.write_properties("/tmp/foo.xml", "/sim/model");  
print(io.write_properties(path, prop));
|example2 = var path = getprop("/sim/fg-home") ~ '/Export/demo.xml';
var prop = props.globals.getNode("position");
print(io.write_properties(path, prop));
|example3 = var path = getprop("/sim/fg-home") ~ '/Export/demo.xml';
var tree = {
    "a": 12.34,
    "b": "Hello, World!",
    "c": {
        "d": [1, 2, 3]
    }
};
var prop = props.Node.new(tree);
print(io.write_properties(path, prop));
}}
}}