I have the following part of an XML file:
<properties>
<project.version>module.version</project.version>
<ie.version>17-8-103</ie.version>
<leg_uk.version>17-6-6</leg_uk.version>
<leg_na.version>17-8-103</leg_na.version>
</properties>
I want to generate a file with the following content:
ie.project.version = 17-8-103
leg_uk.project.version = 17-8-103
How to generate such file?
Is that what You're looking for?
def txt = """<properties>
<project.version>module.version</project.version>
<ie.version>17-8-103</ie.version>
<leg_uk.version>17-6-6</leg_uk.version>
<leg_na.version>17-8-103</leg_na.version>
</properties>"""
def xml = new XmlParser().parseText(txt)
new File('tmp').withWriter { w ->
xml.children().each {
w << "${it.name()}=${it.value().text()}\n"
}
}
Related
I'm trying to access a variable in a nextflow.config file during the execution of a pipeline. I want to supply image_standard as a string in run.nf and I want to receive eu.gcr.io/proj_name/image1:latest as an output. I figured out a way to obtain the content of the .config file within the nextflow script, but I don't know how to access this specific property.
This is my nextflow.config file:
process {
withLabel: image_standard {
container = "eu.gcr.io/proj_name/image1:latest"
}
withLabel: image_deluxe {
container = "eu.gcr.io/proj_name/image2:latest"
}
}
the run.nf
x = workflow.configFiles[0]
Properties properties = new Properties()
File propertiesFile = new File("${x}")
propertiesFile.withInputStream {
properties.load(it)
}
log.info "${properties.process}"
Which just prints the line:
{
You could try instead slurping the config file and selecting the process you want using the ProcessConfig class and the applyConfigSelector() method:
import nextflow.config.ConfigParser
import nextflow.script.ProcessConfig
def config_file = file("${baseDir}/nextflow.config")
def config = new ConfigParser().setIgnoreIncludes(true).parse(config_file.text)
def process = new ProcessConfig([:])
process.applyConfigSelector(config.process, 'withLabel:', 'image_standard')
println(process.container)
i have a xml from where i wish to remove few nodes. The idea i used is to run through all the root node children and keep writing to another file those nodes which i dont need to delete.
One problem I see is: the node attributes gets reordered in the written file which i dont want
my code looks like:
def xml = new XmlSlurper(false, false).parse(args[0])
ant.delete(file:fileName)
File f = new File(fileName)
xml.children().each{
String str = it.#name
if(some condiotion == false)
f << groovy.xml.XmlUtil.serialize(it)
}
another problem is that in the begining of every node it inserts
<?xml version="1.0" encoding="UTF-8"?>
There is no concrete example of the xml present in the question. Here is an example how a Node can be removed:
import groovy.xml.XmlUtil
def xml = '''
<School>
<Classes>
<Class>
<Teachers>
<Name>Rama</Name>
<Name>Indhu</Name>
</Teachers>
<Name>Anil</Name>
<RollNumber>16</RollNumber>
</Class>
<Class>
<Teachers>
<Name>Nisha</Name>
<Name>Ram</Name>
</Teachers>
<Name>manu</Name>
<RollNumber>21</RollNumber>
</Class>
</Classes>
</School>
'''
def parsed = new XmlSlurper().parseText( xml )
parsed.'**'.each {
if(it.name() == 'Teachers') {
it.replaceNode { }
}
}
XmlUtil.serialize( parsed )
In the above example, Teachers node is removed by doing a depthFirst search and iterating over each node and finally using replaceNode with an empty Closure. I hope this can be used as the logic you want.
PS: I have omitted the File operations for brevity.
The API work with a replacementStack. So, the replaceNode {} will show the result only when you serialize the node like:
GPathResult body = parsePath.Body
int oldSize = parsePath.children().size()
body.list()
body[0].replaceNode {} // Remove o no, mas não será visivel para o objeto pai, somente no momento de serializacao. Pois a API adiciona em uma pilha de alteracao que será usada na serializacao
String newXmlContent = XmlUtil.serialize(parsePath)
GPathResult newParsePath = new XmlSlurper().parseText(newXmlContent)
int newSize = newParsePath.children().size()
assertNotNull(this.parsePath)
assertEquals(2, oldSize)
assertEquals(1, newSize)
assertTrue(newSize < oldSize)
assertNotNull(body)
I am new to groovy and here is my question
def config = new ConfigSlurper().
parse(new File('RegionConfig.groovy').toURI().toURL())
Now i need something like
for(String name : listOfNames){
println(config.name)
}
How can i achieve this ?
Like this?
config.groovy:
user.name='koji'
user.nation='japan'
a.b.c='foo'
test code:
def config = new ConfigSlurper().parse(new File('config.groovy').toURL())
assert ['user.name', 'user.nation', 'a.b.c'] == config.flatten().keySet().collect {it as String}
also you can write like follows:
for (String name: config.flatten().keySet()) {
println name
}
i have the following xml to be parsed
<config>
<ParametricTesting>Y</ParametericTesting>
<FunctionalTesting>Y</FunctionalTesting>
<Utilities>N</Utilities>
<CommonApi>N</CommonApi>
<ClientData>N</ClientData>
<DataSourceTest>Y<DataSourceTest>
<Excel>
<ExcelFilePath>myexcel1.xlsx</ExcelFilePath>
</Excel>
<Access>
<AccessDB> </AccessDB>
</Access>
<Sql>
<SqlConnectionString> </SqlConnectionString>
</Sql>
<RunnerConsole>N</RunnerConsole>
<Schedular>N</Schedular>
</config>
I am using xmlreader to read the xml but since i am new to c# so i don't know why the code is breaking after reading second tag i.e ParametericTesting.
code:
string ConfigXml = Path.GetFullPath("Config.xml");
XmlReader xmlReader = XmlReader.Create(ConfigXml);
while (xmlReader.Read()) {
if ((xmlReader.NodeType== XmlNodeType.Element) && xmlReader.Name.Equals("ParametricTesting")) {
// TODO : write code relevant for parametric testing
xmlReader.Read();
}
else if ((xmlReader.NodeType== XmlNodeType.Element)&& xmlReader.Name.Equals("DataSourceTest")) {
string Datasource = xmlReader.GetAttribute("DataSourceTest");
if (Datasource.Equals("Y")) {
if (xmlReader.Name.Equals("Excel") && (xmlReader.NodeType == XmlNodeType.Element)) {
string excelfile = xmlReader.GetAttribute("ExcelFilePath");
string ExcelPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Files\\" + excelfile;
objExcel.DataSourceName = excelfile;
objExcel.Open();
}
}
xmlReader.Read();
}
xmlReader.Read();
}
My code is not reading element beyond parametricTesting . Please help.
you open tag of "ParametricTesting" in the config.xml is different from the closing tag. correct it and that line passes.
also, you don't close the tag "DataSourceTest"
here is the fixed XML:
<config>
<ParametricTesting>Y</ParametricTesting>
<FunctionalTesting>Y</FunctionalTesting>
<Utilities>N</Utilities>
<CommonApi>N</CommonApi>
<ClientData>N</ClientData>
<DataSourceTest>Y</DataSourceTest>
<Excel>
<ExcelFilePath>myexcel1.xlsx</ExcelFilePath>
</Excel>
<Access>
<AccessDB> </AccessDB>
</Access>
<Sql>
<SqlConnectionString> </SqlConnectionString>
</Sql>
<RunnerConsole>N</RunnerConsole>
<Schedular>N</Schedular>
</config>
I am reading a xml file in groovy something like below, but don't know what is going wrong.
Below is my function
def setEnvironment(Map env,Map params)
{
def records=new XmlSlurper().parse(env.get("ABC_HOME")+"/isp/config/nodemeta.xml")
def jdbcurl=records.'domainservice:DBConnectivity'[0].#dbConnectString
params.put("dn", records.'domainservice:GatewayNodeConfig'[0].#domainName)
params.put("dh", records.'domainservice:GatewayNodeConfig'[0].'address'[0].#host)
params.put("dp", records.'domainservice:GatewayNodeConfig'[0].'address'[0].#httpPort)
params.put("u", records.'domainservice:DBConnectivity'[0].#dbUsername)
if(jdbcurl==null||jdbcurl.size()==0)
{
params.put("tns", records.'domainservice:DBConnectivity'[0].#dbHost)
}
else
{
params.put("tns", jdbcurl.find("(?<=%2F%2F)[\\d\\w_]+"))
}
println params
}
My Output
[pd:admin, u:, tns:, dh:, dn:, dp:, un:admin, x:c1164035531]
My Xml
<?xml version="1.0" encoding="UTF-8"?>
<imx:IMX xmlns:imx="http://com.abc.imx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" serializationSpecVersion="4.0" crcEnabled="0" xmlns:domainservice="http://com.abc.isp.metadata.domainservice/2" versiondomainservice="2.4.1" xmlns:common="http://com.abc.isp.metadata.common/2" versioncommon="2.2.0" xsi:schemaLocation="http://com.abc.imx IMX.xsd http://com.abc.isp.metadata.domainservice/2 com.abc.isp.metadata.domainservice.xsd http://com.abc.isp.metadata.common/2 com.abc.isp.metadata.common.xsd">
<domainservice:GatewayNodeConfig imx:id="U:LtWHxY0ZEeGb2FwhP-xoGw" adminconsolePort="15533" adminconsoleShutdownPort="38207" domainName="D_1035531" nodeName="N_1035531" dbConnectivity="ID_1">
<address imx:id="ID_2" xsi:type="common:NodeAddress" host="absdie" httpPort="1531" port="1532"/>
<portals>
<NodeRef imx:id="ID_3" xsi:type="common:NodeRef" address="ID_2" nodeName="N_1035531"/>
</portals>
</domainservice:GatewayNodeConfig>
<domainservice:DBConnectivity imx:id="ID_1" dbEncryptedPassword="ZmTXZDoYq0TyrU7fSaS9BrAlIuZyS2rw%2FafW1TLWE4g%3D" dbHost="fortuner" dbName="ORCL" dbPort="1521" dbType="ORACLE" dbUsername="zx1649355388"/>
</imx:IMX>
Note: pd & x are already there in the map
XmlSlurper is handling the namespaces for you... Just get rid of the namespace part of the node names (note the more Groovy map management as well)
params.dn = records.GatewayNodeConfig[0].#domainName.text()
params.dh = records.GatewayNodeConfig[0].address[0].#host.text()
params.dp = records.GatewayNodeConfig[0].address[0].#httpPort.text()
params.u = records.DBConnectivity[0].#dbUsername.text()