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: [EMAIL PROTECTED]
Lihovarská 1060/12 tel: +420 284 028 960
190 00 Praha 9, Czech Republic http://www.suse.cz
<?xml version="1.0"?>
<image name="openSUSE-10.3 YaST testing" schemeversion="1.4">
<preferences>
<type primary="true" boot="isoboot/suse-10.3" flags="unified">iso</type>
<type boot="oemboot/suse-10.3" filesystem="ext3">oem</type>
<version>2.5</version>
<size unit="M">2000</size>
<defaultdestination>/tmp/out</defaultdestination>
</preferences>
<packages type="image" patternType="onlyRequired" patternPackageType="plusRecommended" profiles="KDE">
<opensusePattern name="kde"/>
<package name="OpenOffice_org"/>
</packages>
</image>
$[
"image" : [
$[
"name" : "openSUSE-10.3 YaST testing",
"packages" : [
$[
"opensusePattern" : [
$[
"name" : "kde"
]
],
"package" : [
$[
"name" : "OpenOffice_org"
]
],
"patternPackageType" : "plusRecommended",
"patternType" : "onlyRequired",
"profiles" : "KDE",
"type" : "image"
]
],
"preferences" : [
$[
"defaultdestination" : [
$[
"content" : "/tmp/out"
]
],
"size" : [
$[
"content" : 2000,
"unit" : "M"
]
],
"type" : [
$[
"boot" : "isoboot/suse-10.3",
"content" : "iso",
"flags" : "unified",
"primary" : true
],
$[
"boot" : "oemboot/suse-10.3",
"content" : "oem",
"filesystem" : "ext3"
]
],
"version" : [
$[
"content" : "2.5"
]
]
]
],
"schemeversion" : "1.4"
]
]
]