[yast-devel] new SCR agent for xml (again)
Hi hackers, since yast2-2.16.57 you can use a new agent for reading and writing XML files from YaST. The SCR path is .anyxml - yes, it is same as the one of previous agent that we had before (it was announced in http://lists.opensuse.org/yast-devel/2007-11/msg00057.html). The old agent was found incapable for parsing more than very simple files and was renamed to .barexml, see bug 366867 for details. The new agent is using Perl XML::Simple library and is able to return whole content of XML file in a tree structure. Usage: Read a file /tmp/in.xml into YCP map: map config = (map)SCR::Read (.anyxml, $[ "file" : "/tmp/in.xml", "args" : $[ "ForceArray" : 0, "KeepRoot" : 1 ] ]); See the attached example XML file and it's YCP representation got as a result of this command. You can see that the call can take some optional arguments, the meaning of those 2 in example is "force nested elements to be represented as arrays even when there is only one" and "retain the root element name". The agent should actually be able to parse most[*] of the options that XML::Simple provides, see http://search.cpan.org/dist/XML-Simple/lib/XML/Simple.pm for more. Writing modified YCP structure back to XML is also easy: SCR::Write (.anyxml, $[ "file" : "/tmp/config.xml", "xml" : config, ]); Find a bit more about usage directly in the agent file: http://svn.opensuse.org/svn/yast/trunk/yast2/library/agents/ag_anyxml Jiri [*] = not tested -- Jiri Suchomel SUSE LINUX, s.r.o. e-mail: jsuchome@suse.cz Lihovarská 1060/12 tel: +420 284 028 960 190 00 Praha 9, Czech Republic http://www.suse.cz
On 29/04/2008, Jiří Suchomel <jsuchome@suse.cz> wrote:
Hi hackers,
since yast2-2.16.57 you can use a new agent for reading and writing XML files from YaST. The SCR path is .anyxml -
Finally, :) Only thing I noticed in testing .anyxml is that it is easy to write invalid XML if you don't consider the XML when creating the YCP data structure. For example, the following will generate invalid XML: { map xml = $[ "xmltest": $[ "123one" : $[ "alpha": "alphavalue", "beta": "betavalue", "content": "onecontent" ] ] ]; SCR::Write ( .anyxml, $[ "file": "/tmp/anyxmltest.xml", "xml" : xml ] ); } Since an XML node name must start with a letter. I don't know if there's a way for you to work around this. In other platforms XML marshalling/unmarshalling facilities usually take care that valid XML is always produced, and can be read back into the same structure. I was using Perl XML simple originally for handling XML with metapackage-handler, but changed to Perl XML XPath as dealing with large structures of nested lists of maps of lists of maps became rather verbose and difficult to follow. Even with ForceArray=1 it is possible for two valid instances of the same schema to generate different map/list structures. It would be nice to switch to using an SCR agent for metapackage-handler, but I think it may be impractical to use the XML::Simple based agent. Perhaps I should look at making an XML::XPath agent sometime after 11.0. -- Benjamin Weber
On út 29. dubna 2008, Benji Weber wrote:
On 29/04/2008, Jiří Suchomel <jsuchome@suse.cz> wrote:
Hi hackers,
since yast2-2.16.57 you can use a new agent for reading and writing XML files from YaST. The SCR path is .anyxml -
Hi Benji, first, thanks for testing :-) I should have written that even this agent is not ideal and not usable for some cases. For example I cannot use it for writing kiwi configuration file as I cannot regulate the order of nodes - so I'm using it only for reading. I still think it may have sense for someone, how is aware of its limitations (which are based on XML::Simple limitations).
It would be nice to switch to using an SCR agent for metapackage-handler, but I think it may be impractical to use the XML::Simple based agent. Perhaps I should look at making an XML::XPath agent sometime after 11.0.
If it would solve current problems, it would be really welcomed. Jiri
Benjamin Weber
-- Jiri Suchomel SUSE LINUX, s.r.o. e-mail: jsuchome@suse.cz Lihovarská 1060/12 tel: +420 284 028 960 190 00 Praha 9, Czech Republic http://www.suse.cz -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
participants (2)
-
Benji Weber
-
Jiří Suchomel