Get Comment in XML file by RapidXML - comments

I try to get value of a command in xml file. I found the function type() on its manual. But it just return numberic value about type of node. Are there any ways to get it?
This is my snippet:
xml_node<> *Node = Doc.first_node();
xml_node<> *Sub = Node->first_node("Task");
std::cout << "Comment: " << Sub->type() << std::endl;
And the value return is: Comment: 1
This is file's content:
<!-- \GoogleUpdateTaskMachineCore --> ***<<== this is the value I wanna get.***
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Version>1.3.33.7</Version>
<Description>Keeps your Google software up to date. If this task is disabled or stopped, your Google software will not be kept up to date, meaning security vulnerabilities that may arise cannot be fixed and features may not work. This task uninstalls itself when there is no Google software using it.</Description>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
</LogonTrigger>
<CalendarTrigger>
<StartBoundary>2017-11-14T12:07:01</StartBoundary>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<Enabled>true</Enabled>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\Program Files (x86)\Google\Update\GoogleUpdate.exe</Command>
<Arguments>/c</Arguments>
</Exec>
</Actions>
</Task>

First, you'll need need to use the parse_comment_nodes flag when you parse() the document.
Then, iterate through the nodes until you find a node with type() == node_comment, then get the value() of that node.

Related

How to edit vsixmanifest using groovy without loosing content?

My question may sound stupid but I am struggling to achieve my result. I would like to edit and save version tage of vsixmanifest file without loosing any content. This article almost solved my purpose but it removes some of the tags.
<?xml version="1.0" encoding="utf-8"?>
<Vsix xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Version="1.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010">
<Identifier Id="Visual Test">
<Name>Visual Gallery Test</Name>
<Author>Visual Studio Demo</Author>
<Version>XXXXX</Version>
<Description xml:space="preserve">Visual Studio Gallery Demo</Description>
<Locale>1033</Locale>
<AllUsers>true</AllUsers>
<InstalledByMsi>false</InstalledByMsi>
<Icon>Resources/Icon.png</Icon>
<PreviewImage>Resources/Preview.png</PreviewImage>
<SupportedProducts>
<IsolatedShell Version="7.0">Visual Studio</IsolatedShell>
</SupportedProducts>
<SupportedFrameworkRuntimeEdition MinVersion="4.6" MaxVersion="4.9" />
</Identifier>
<Content>
<VsPackage>XX.pkgdef</VsPackage>
</Content>
</Vsix>
Here is my gradle script
task updateExtensionManifest{
def vsixmanifestFile = "build/source.extension.vsixmanifest"
def vsixmanifest = new XmlParser().parse(vsixmanifestFile)
vsixmanifest.Identifier[0].Version[0].value="YYYYY"
def nodePrinter = new XmlNodePrinter(new PrintWriter(new FileWriter(vsixmanifestFile)))
nodePrinter.preserveWhitespace = true
nodePrinter.expandEmptyElements = true
nodePrinter.print(vsixmanifest)
}
When I execute the script it removes some of the tags defines manifest file, this is how it looks after task is getting executed:
<Vsix xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010" Version="1.0.0">
<Identifier Id="Visual Test">
<Name>Visual Gallery Test</Name>
<Author>Visual Studio Demo</Author>
<Version>YYYYY</Version>
<Description xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">Visual Studio Gallery Demo</Description>
<Locale>1033</Locale>
<AllUsers>true</AllUsers>
<InstalledByMsi>false</InstalledByMsi>
<Icon>Resources/Icon.png</Icon>
<PreviewImage>Resources/Preview.png</PreviewImage>
<SupportedProducts>
<IsolatedShell Version="7.0">Visual Studio</IsolatedShell>
</SupportedProducts>
<SupportedFrameworkRuntimeEdition MinVersion="4.6" MaxVersion="4.9"></SupportedFrameworkRuntimeEdition>
</Identifier>
<Content>
<VsPackage>XX.pkgdef</VsPackage>
</Content>
</Vsix>
Some unwanted edits:
Line1 removed: <?xml version="1.0" encoding="utf-8"?>
Line2 modified to <Vsix xmlns="http://schemas.microsoft.com/developer/vsx-schema/2010" Version="1.0.0">
Tag "Description" got edited too..
How could I avoid this edits? I just expected the version to get modified from XXXXX to YYYYY without changing any other content through my gradle build script.
That is because XmlNodePrinter.
If xml markup declaration is needed, use XmlUtil.serialize().
def vsixmanifest = new XmlSlurper().parse(vsixmanifestFile)
vsixmanifest.Identifier[0].Version.replaceBody ( "YYYYY" )
println groovy.xml.XmlUtil.serialize(vsixmanifest)
You can quickly try the demo

Using nokogiri xpath to access nested elements within an xmlns

I'm new to nokogiri and am having trouble using xpath to access nested elements of an xml document with a specific xmlns.
Given the following code
#!/opt/chef/embedded/bin/ruby
require 'nokogiri'
doc = Nokogiri::XML.parse <<-XML
<?xml version="1.0" encoding="UTF-8" ?>
<domain xmlns="urn:jboss:domain:1.8">
<profiles>
<profile name="full">
<subsystem xmlns="urn:jboss:domain:datasources:1.2">
<datasources>
<datasource jndi-name="java:/Paulstestjndi" pool-name="pauls_ds" enabled="false">
<connection-url>jdbc:oracle:thin:#testhost1:80001paulstestinstance|jdbc:oracle:thin:#testhost2:80001paulstestinstance</connection-url>
</datasource>
</datasources>
</subsystem>
</profile>
</profiles>
</domain>
XML
datasources = doc.xpath('//datasources:datasource', 'datasources' => "urn:jboss:domain:datasources:1.2")
datasources.each do |datasource|
conn_url = datasource.xpath("connection-url")
puts "CLASS = #{conn_url.class}"
puts "No of Entries = #{conn_url.length}"
end
I am able to retrieve datasources using xpath but am unable to use xpath to access 'connection-url' for each datasource.
I have tried several xpath calls to achieve this the following are examples
conn_url = datasource.xpath("connection-url")
conn_url = datasource.xpath("//connection-url")
conn_url = datasource.xpath("//datasources:datasource/connection-url", 'datasources'=>"urn:jboss:domain:datasources:1.2")
But each seems to return an empty set of results.
What am I missing?
It’s a namespacing issue:
datasource.xpath(
'subsystem:connection-url',
'subsystem' => 'urn:jboss:domain:datasources:1.2')
#⇒ [#<... name="connection-url" namespace=...

How can I get a pathname from a groupId? [duplicate]

Is there a simple way of taking the value of a property and then copy it to another property with certain characters replaced?
Say propA=This is a value. I want to replace all the spaces in it into underscores, resulting in propB=This_is_a_value.
Here is the solution without scripting and no external jars like ant-conrib:
The trick is to use ANT's resources:
There is one resource type called "propertyresource" which is like a source file, but provides an stream from the string value of this resource. So you can load it and use it in any task like "copy" that accepts files
There is also the task "loadresource" that can load any resource to a property (e.g., a file), but this one could also load our propertyresource. This task allows for filtering the input by applying some token transformations. Finally the following will do what you want:
<loadresource property="propB">
<propertyresource name="propA"/>
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from=" " to="_"/>
</tokenfilter>
</filterchain>
</loadresource>
This one will replace all " " in propA by "_" and place the result in propB. "filetokenizer" treats the whole input stream (our property) as one token and appies the string replacement on it.
You can do other fancy transformations using other tokenfilters: http://ant.apache.org/manual/Types/filterchain.html
Use the propertyregex task from Ant Contrib.
I think you want:
<propertyregex property="propB"
input="${propA}"
regexp=" "
replace="_"
global="true" />
Unfortunately the examples given aren't terribly clear, but it's worth trying that. You should also check what happens if there aren't any underscores - you may need to use the defaultValue option as well.
If ant-contrib isn't an option, here's a portable solution for Java 1.6 and later:
<property name="before" value="This is a value"/>
<script language="javascript">
var before = project.getProperty("before");
project.setProperty("after", before.replaceAll(" ", "_"));
</script>
<echo>after=${after}</echo>
In case you want a solution that does use Ant built-ins only, consider this:
<target name="replace-spaces">
<property name="propA" value="This is a value" />
<echo message="${propA}" file="some.tmp.file" />
<loadfile property="propB" srcFile="some.tmp.file">
<filterchain>
<tokenfilter>
<replaceregex pattern=" " replace="_" flags="g"/>
</tokenfilter>
</filterchain>
</loadfile>
<echo message="$${propB} = "${propB}"" />
</target>
Output is ${propB} = "This_is_a_value"
Use some external app like sed:
<exec executable="sed" inputstring="${wersja}" outputproperty="wersjaDot">
<arg value="s/_/./g"/>
</exec>
<echo>${wersjaDot}</echo>
If you run Windows get it googling for "gnuwin32 sed".
The command s/_/./g replaces every _ with .
This script goes well under windows. Under linux arg may need quoting.
Two possibilities :
via script task and builtin javascript engine (if using jdk >= 1.6)
<project>
<property name="propA" value="This is a value"/>
<script language="javascript">
project.setProperty('propB', project.getProperty('propA').
replace(" ", "_"));
</script>
<echo>$${propB} => ${propB}</echo>
</project>
or using Ant addon Flaka
<project xmlns:fl="antlib:it.haefelinger.flaka">
<property name="propA" value="This is a value"/>
<fl:let> propB := replace('${propA}', '_', ' ')</fl:let>
<echo>$${propB} => ${propB}</echo>
</project>
to overwrite exisiting property propA simply replace propB with propA
Here's a more generalized version of Uwe Schindler's answer:
You can use a macrodef to create a custom task.
<macrodef name="replaceproperty" taskname="#{taskname}">
<attribute name="src" />
<attribute name="dest" default="" />
<attribute name="replace" default="" />
<attribute name="with" default="" />
<sequential>
<loadresource property="#{dest}">
<propertyresource name="#{src}" />
<filterchain>
<tokenfilter>
<filetokenizer/>
<replacestring from="#{replace}" to="#{with}"/>
</tokenfilter>
</filterchain>
</loadresource>
</sequential>
</macrodef>
you can use this as follows:
<replaceproperty src="property1" dest="property2" replace=" " with="_"/>
this will be pretty useful if you are doing this multiple times
Adding an answer more complete example over a previous answer
<property name="propB_" value="${propA}"/>
<loadresource property="propB">
<propertyresource name="propB_" />
<filterchain>
<tokenfilter>
<replaceregex pattern="\." replace="/" flags="g"/>
</tokenfilter>
</filterchain>
</loadresource>
Just an FYI for answer Replacing characters in Ant property - if you are trying to use this inside of a maven execution, you can't reference maven variables directly. You will need something like this:
...
<target>
<property name="propATemp" value="${propA}"/>
<loadresource property="propB">
<propertyresource name="propATemp" />
...
Properties can't be changed but antContrib vars (http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html ) can.
Here is a macro to do a find/replace on a var:
<macrodef name="replaceVarText">
<attribute name="varName" />
<attribute name="from" />
<attribute name="to" />
<sequential>
<local name="replacedText"/>
<local name="textToReplace"/>
<local name="fromProp"/>
<local name="toProp"/>
<property name="textToReplace" value = "${#{varName}}"/>
<property name="fromProp" value = "#{from}"/>
<property name="toProp" value = "#{to}"/>
<script language="javascript">
project.setProperty("replacedText",project.getProperty("textToReplace").split(project.getProperty("fromProp")).join(project.getProperty("toProp")));
</script>
<ac:var name="#{varName}" value = "${replacedText}"/>
</sequential>
</macrodef>
Then call the macro like:
<ac:var name="updatedText" value="${oldText}"/>
<current:replaceVarText varName="updatedText" from="." to="_" />
<echo message="Updated Text will be ${updatedText}"/>
Code above uses javascript split then join, which is faster than regex. "local" properties are passed to JavaScript so no property leakage.
Or... You can also to try Your Own Task
JAVA CODE:
class CustomString extends Task{
private String type, string, before, after, returnValue;
public void execute() {
if (getType().equals("replace")) {
replace(getString(), getBefore(), getAfter());
}
}
private void replace(String str, String a, String b){
String results = str.replace(a, b);
Project project = getProject();
project.setProperty(getReturnValue(), results);
}
..all getter and setter..
ANT SCRIPT
...
<project name="ant-test" default="build">
<target name="build" depends="compile, run"/>
<target name="clean">
<delete dir="build" />
</target>
<target name="compile" depends="clean">
<mkdir dir="build/classes"/>
<javac srcdir="src" destdir="build/classes" includeantruntime="true"/>
</target>
<target name="declare" depends="compile">
<taskdef name="string" classname="CustomString" classpath="build/classes" />
</target>
<!-- Replacing characters in Ant property -->
<target name="run" depends="declare">
<property name="propA" value="This is a value"/>
<echo message="propA=${propA}" />
<string type="replace" string="${propA}" before=" " after="_" returnvalue="propB"/>
<echo message="propB=${propB}" />
</target>
CONSOLE:
run:
[echo] propA=This is a value
[echo] propB=This_is_a_value

JAXB Moxy getValueByXpath gives null

I want to see if a theme element exists with specified name in the following xml file.
input.xml
<data>
<artifacts>
<document id="efqw4eads">
<name>composite</name>
</document>
<theme id="1">
<name>Terrace</name>
</theme>
<theme id="2">
<name>Garage</name>
</theme>
<theme id="3">
<name>Reception</name>
</theme>
<theme id="4">
<name>Grade II</name>
</theme>
</artifacts>
</data>
I have the following code. return true statement of the method is never executed. answer always contains a null value.
public boolean themeExists(String name, Data data){
String expression = "artifacts/theme[name='"+name+"']/name/text()";
String answer = jaxbContext.getValueByXPath(data, expression, null, String.class);
if(answer == null || answer.equals("")){
return false;
}
return true;
}
This use case isn't currently supported by EclipseLink JAXB (MOXy). I have opened the following enhancement you can use to track our progress:
http://bugs.eclipse.org/413823
There is no <artifacts/> element you're look for in the first axis step. Your XPath expression should be something like
String expression = "data/theme[name='"+name+"']/name/text()";

nlog using the connectionStringName for database logging

here is my nlog.config file. I have turned on the throwsException.
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">
<targets>
<target type="Database" name="databaseLog"
dbProvider="sqlserver" connectionstring="server=.\SQLExpress;database=Movie;integrated security=true">
<commandText>
INSERT INTO [Log] ([Description] , [Level] ) VALUES (#Description, #Level )
</commandText>
<parameter name="#Description" layout="${message}"/>
<parameter name="#Level" layout="${level}"/>
</target>
</targets>
<rules>
<logger name="*" minLevel="Trace" appendTo="databaseLog"/>
</rules>
</nlog>
This will work and will insert records into the database. However I would like to use connectionstringName and not retype the connectionstring.
When I change the connectionstring to connectionstringname like this....
connectionstring="server=.\SQLExpress;database=Movie;integrated security=true"
to
connectionStringName="ApplicationConnectionString"
I get an error
Expecting non-empty string for 'providerInvariantName' parameter
Add System.Data.SqlClient to attribute ProviderName in your connection string in web.config/app.config:
<add name="ApplicationConnectionString"
providerName="System.Data.SqlClient"
connectionString="server=.\SQLExpress;database=Movie;integrated security=true;"/>

Resources