How to access XML parameters conditionally - ruby

This is a XML code snippet:
<testcase name="T.3.03.02">
<cmd>CMD_EXPORT_RAM_KEY</cmd>
<sreg_pre>40</sreg_pre>
<sreg_pre_bitmask>ff</sreg_pre_bitmask>
<sreg_post>40</sreg_post>
<sreg_post_bitmask>ff</sreg_post_bitmask>
<erc>ERC_NO_ERROR</erc>
<testvector>
<parameter name="UID" type="info">000000000000000000000000000002</parameter>
<parameter name="UID'" type="info">000000000000000000000000000002</parameter>
<parameter name="KeyId" type="info">0e</parameter>
<parameter name="Key" type="info">0f0e0d0c0b0a09080706050403020100</parameter>
<parameter name="AuthId" type="info">00</parameter>
<parameter name="KeyAuth" type="info">2b7e151628aed2a6abf7158809cf4f3c</parameter>
<parameter name="Old counter value of updated key slot" type="info">0000000</parameter>
<parameter name="New counter value C'" type="info">0000000</parameter>
<parameter name="Protection flags F'" type="info">00</parameter>
<parameter name="M1" type="output">000000000000000000000000000002e0</parameter>
<parameter name="M2" type="output">152876f29dc7ca8d18e38d70374492b05d908c8c584a0409849a553c75254def</parameter>
<parameter name="M3" type="output">bc6e79bc4458339174fc80fb08b83188</parameter>
<parameter name="M4" type="output">000000000000000000000000000002e07783b86ae87b87e3ca12809c2df75fae</parameter>
<parameter name="M5" type="output">c8fcc8859c69c8bd840ce8e24c5114e9</parameter>
</testvector>
<precondition>RAM_KEY_PLAIN = 1; RAM_KEY_EMPTY = 0</precondition>
<description>Export plain RAM_KEY with external debugger attached; Note: The security flags SECURE_BOOT_PROTECTION and DEBUGGER_PROTECTION of the key SECRET_KEY are inherited from MASTER_ECU_KEY.</description>
</testcase>
I want to access all "parameter name="Key" type="info" values.
How do I access these values conditionally if the condition <cmd>CMD_EXPORT_RAM_KEY(second line in XML)</cmd> is valid.
In this XML file there are also other commands (<cmd> lines) also with the "Key" parameter,
but in these cases I don't want to get the key-values.
I didn't get it running.
Can anyone help me with some ideas?

Would something like this work?
doc = Nokogiri::parse(File.read( "data.xml" ))
check = doc.xpath( "//cmd" ).select{|el| el.children[0].text == "CMD_EXPORT_RAM_KEY" }
puts "Check: %i" % check.size
if(check.size == 0)
## Do stuff here
end

Try the below XPath with Nokogiri:
//testcase/cmd[text()='CMD_EXPORT_RAM_KEY']/../testvector/parameter[#name="Key" and #type="info"]
Of course, you can parameterize the CMD_EXPORT_RAM_KEY and #name/#type values.

The trick is to locate the specific ones you want using a selector, then narrow down further if necessary.
Using the CSS selector 'parameter[#name="Key"][#type="info"]' Nokogiri easily finds the single occurrence in your sample. If there were more, then more would be returned:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
<testcase name="T.3.03.02">
<testvector>
<parameter name="UID" type="info">000000000000000000000000000002</parameter>
<parameter name="Key" type="info">0f0e0d0c0b0a09080706050403020100</parameter>
</testvector>
</testcase>
EOT
doc.search('parameter[#name="Key"][#type="info"]').map(&:content)
# => ["0f0e0d0c0b0a09080706050403020100"]
I used CSS because it looks less like line noise than the equivalent XPath selector would.
Also, when supplying sample data, reduce it to the bare minimum necessary to test the code. Anything beyond that wastes our time, and, if it's too much, can actually cause you to get no answers because nobody wants to wade through that.

Related

Why SUMO output different using veins_launchd and without veins_launchd?

First, I use veins_launchd to conect omnet and sumo, the command line is "D:/example_project/veins-5.1/veins-veins-5.1/sumo-launchd.py -vv -c 'D:/Program Files (x86)/Eclipse/bin/sumo.exe' "
The ouput lane data is shown below:
But I want to run SUMO manually, so I modify TraCIScenarioManagerLaunchd to TraCIScenarioManager and use the command line "sumo -c llcd.sumocfg --remote-port 9999", and then waiting for omnet to connected. This approach is inspired by this answer Is there a way to connect multiple TraCI modules to the OMNet++/veins simulation?
But when I run this way, the output result is like this:
Why do simulation results make such a huge difference? I didn't change any other parameters, and the sumo configure file is the same.
some of the parameter setting in omnetpp.ini:
##########################################################
# TraCIScenarioManager parameters #
##########################################################
*.manager.updateInterval = 1s
*.manager.host = "localhost"
*.manager.port = 9999
*.manager.autoShutdown = false
*.manager.launchConfig = xmldoc("llcd.launchd.xml")
sumo.config:
<configuration>
<input>
<net-file value="D:\FTT\sumo_test\llcd\llcd.net.xml"/>
<route-files value="D:\FTT\sumo_test\llcd\llcd.rou.xml"/>
<additional-files value="D:\FTT\sumo_test\llcd\llcd.add.xml"/>
</input>
<output>
<lanechange-output value="D:\FTT\sumo_test\llcd\llcd.lanechange.xml"/>
<summary-output value="D:\FTT\sumo_test\llcd\output_sumo.xml"/>
</output>
<time>
<begin value="0"/>
<end value="3600"/>
</time>
<processing>
<time-to-teleport value="-1"/>
</processing>
<report>
<no-duration-log value="true"/>
<no-step-log value="true"/>
<no-warnings value="true"/>
</report>
</configuration>
If anyone can give some insights? Thank you in advance!
I figured out the problem. I found veins_launchd will change the configure file of SUMO by adding
<random_number>
<random value="false"/>
<seed value="0"/>
</random_number>

Comment .XML file content using ruby code

Can anyone tell me how I can comment the following line in my .XML file using Ruby?
I hope this can be done by using "nokogiri".
<message group="1" sub_group="1" type="none" destination="mydata" remark="mylist" userOnly="true "/>
output should be:
<!-- <message group="1" sub_group="1" type="none" destination="mydata" remark="mylist" userOnly="true "/> -->
You can search your document with the search method, add a comment with Comment.new and then remove the original line with the remove method.
Nokogiri::XML::Comment.new(doc, node.to_s)
Class: Nokogiri::XML::Comment
Edit:
I implemented an example, but used replace instead of remove:
require 'nokogiri'
f = File.open('./config.xml')
x = Nokogiri::XML(f);
x.search('message').each do |el|
puts(el.to_s)
c = Nokogiri::XML::Comment.new(x, el.to_s);
el.replace(c);
end
File.write('./config.xml', x.to_xml);

Whats the applescript syntax for sending arguments?

I'm following apples applescript example for scripting parameters
I can send the direct parameter no problem using the following syntax
tell app "SimpleScriptingVerbs" to do command with args "Im a direct parameter"
However I can't figure out the correct syntax for sending the other optional arguments.
The rest of the accepted optional arguments look like this
<command name="do command with args" code="SVrbAgCm" description="run a command with a bunch of arguments">
<cocoa class="CommandWithArgs"/>
<direct-parameter description="a text parameter passed to the command">
<type type="text"/>
</direct-parameter>
<parameter name="blinking" code="savo" type="boolean" optional="yes"
description="a boolean parameter.">
<cocoa key="SaveOptions"/>
</parameter>
<parameter name="preferred hand" code="LRnd" type="preferredhands" optional="yes"
description="a parameter using our enumeration.">
<cocoa key="TheHand"/>
</parameter>
<parameter name="prose" code="Pros" type="text" optional="yes"
description="a text parameter.">
<cocoa key="ProseText"/>
</parameter>
<parameter name="ivalue" code="iVal" type="integer" optional="yes"
description="an integer parameter.">
<cocoa key="IntegerValue"/>
</parameter>
<parameter name="rvalue" code="rVal" type="real" optional="yes"
description="an real number parameter.">
<cocoa key="RealValue"/>
</parameter>
<result type="text" description="the direct parameter enclosed in quotes"/>
</command>
What's the correct applescript syntax to send the rest of these arguments.
You must use a parameter's name with a value, like this:
tell application "SimpleScriptingVerbs" to do command with args "Something" prose "bla 1" preferred hand Left Hand ivalue 299 rvalue 75.777
The application logs -->
proc=-[CommandWithArgs performDefaultImplementation] The other parameters are: '{
"" = Something;
IntegerValue = 299;
ProseText = "bla 1";
RealValue = "75.777";
TheHand = 1279816302;
}'

Replacing a content of a XML Tag [duplicate]

I'm trying to figure out how to open an xml file, search by an id, replace a value in the node and then resave the document.
my xml
<?xml version="1.0"?>
<data>
<user id="1370018670618">
<email>1#1.com</email>
<sent>false</sent>
</user>
<user id="1370018701357">
<email>2#2.com</email>
<sent>false</sent>
</user>
<user id="1370018769724">
<email>3#3.com</email>
<sent>false</sent>
</user>
<user id="1370028546850">
<email>4#4.com</email>
<sent>false</sent>
</user>
<user id="1370028588345">
<email>5#5.com</email>
<sent>false</sent>
</user>
</data>
My code to open and find a node
xml_content = File.read("/home/mike/app/users.xml")
doc = Nokogiri::XML(xml_content)
node_update = doc.search("//user[#id='1370028588345'] //sent")
node_update.inner_html ##returns value of "sent"
the part in this where I'm stuck is actually updating the node. node_update.inner_html = "true" returns a method error on inner_html. then after that saving the updated file.
First of all, your node_update is actually a NodeSet, not the Node that you probably think it is. You need a Node if you want to call inner_html= on it:
node_update[0].inner_html = 'true'
Then writing out the updated XML is just a bit of standard file manipulating combined with a to_xml call:
File.open('whatever.xml', 'w') { |f| f.print(doc.to_xml) }
As an aside, your input isn't valid XML. You have a </details> but no <details>.

AppleScript script command implementation

I'm trying to implement a trivial script command, byt have no success..
My sdef file
...
<class name="image" plural="images" code="imag" description="Image class">
<cocoa class="MyImage" />
<property name="width" code="wdth" type="real" access="r" description="The width of the image."/>
<property name="height" code="hght" type="real" access="r" description="The height of the image."/>
<responds-to name="rotate">
<cocoa method="scriptingRotate:"/>
</responds-to>
</class>
<command name="rotate" code="frwkrota" description="Rotate the image.">
<direct-parameter type="image"/>
<parameter name="by" code="by " type="real" description="Degrees to rotate.">
<cocoa key="angle"/>
</parameter>
</command>
...
My dictionary is ok: image responds to rotate and so on..
But
tell application "MyApp"
rotate image 1 by 1
end tell
says that: "Expected end of line but found class name." Where is the mistake?
get image 1 works normally (MyImage have objectSpecifier).
SOLVED
Seems like it was a bug with ScriptEditor..
Restarting ScriptEditor solved the problem.

Resources