how cereal load or save XML attribute - c++11

I'm trying to save/load XML file use cereal, but can not figure out how to save/load XML attribute. For example:
I have a XML file:
<windows height="101", width = "200"/>
and a struct
struct window
{
int height;
int width;
}
how can i define serialize function to load/save XML attribute value using cereal?

cereal is not a general purpose XML parser. The XML archive that comes with cereal expects to be reading in XML that it itself generates, or at the very least XML in an identical format to what it expects.
For your example, cereal would expect as input:
<?xml version="1.0" encoding="utf-8"?>
<cereal>
<windows>
<height>101</height>
<width>200</width>
</windows>
</cereal>
To get the behavior you desire you would need to modify the XML archive or create your own archive.

Related

Update parameter value in XML format

I have parameters stored in an XML file. Below is a sample of the file.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<terminal id="A">
<terminalCapacity>3</terminalCapacity>
<terminalMembers id="1">
<memberID>0001</memberID>
<memberCapacity>2</memberCapacity>
</terminalMembers>
</terminal>
<terminal id="B">
<terminalCapacity>4</terminalCapacity>
<terminalMembers id="1">
<memberID>0002</memberID>
<memberCapacity>1</memberCapacity>
</terminalMembers>
<terminalMembers id="2">
<memberID>0003</memberID>
<memberCapacity>3</memberCapacity>
</terminalMembers>
</terminal>
</root>
Each terminalID is associated to a type of simpleModule found in my NED file. The idea is to programmatically update these values throughout the simulation run. The current logic revolves around getting the current parameters in XML format and update the memberCapacity field.
From the Omnet cPar and cXMLElement documentation, I tried using the par("moduleParameter").xmlValue()->getXML() function, but this returns the XML as a string. I also tried using the getAttribute() function, but to no success.
Don't do this. par("moduleParameter").xmlValue() will give you the in memory object tree of the XML document, but that is not meant for modification. Your XML file seems to be just a hierarchical structure and modules and their parameters can mirror that exactly. There is absolutely no reason to reinvent the wheel when you can mirror that with INI file parameters.

json tag in .proto file

I am trying to generate go structs incl. json tags.
I can do:
string name = 1 [json_name="item.name, omitempty"];
But that only generates - json=item.name and only json:"name,omitempty"
Name string `protobuf:"bytes,1,opt,name=name,json=item.name, omitempty,proto3" json:"name,omitempty"`
But I need:- json:"item.name,omitempty" Note the tag is missing "item.".
And it looks like no matter what you put in json_name it is not reflected in "json:"
Du you know how to set json tags?
Not supported. The proto file json_name is not used to generate the code.
https://github.com/golang/protobuf/issues/52
I also thought it was a bug
https://github.com/golang/protobuf/issues/998

How to extract values (list and unique tags) from xml in Nifi?

I have a xml file that contains some tags, one is unique other are same tags. I'd like to get values from these tags and convert them to another xml tag. Here is example of xml file:
<?xml version="1.0" encoding="UTF-8"?>
<entry dataset="Swiss-Prot" created="2005-04-26" modified="2019-11-13" version="158" xmlns="http://uniprot.org/uniprot" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<accession>Q96IY4</accession>
<accession>A8K464</accession>
<accession>Q15114</accession>
<accession>Q5T9K1</accession>
<accession>Q5T9K2</accession>
<accession>Q9P2Y6</accession>
<name>CBPB2_HUMAN</name>
<protein>
<recommendedName>
<fullName>Carboxypeptidase B2</fullName>
<ecNumber evidence="7">3.4.17.20</ecNumber>
</recommendedName>
<alternativeName>
<fullName>Carboxypeptidase U</fullName>
<shortName>CPU</shortName>
</alternativeName>
<alternativeName>
<fullName>Plasma carboxypeptidase B</fullName>
<shortName>pCPB</shortName>
</alternativeName>
<alternativeName>
<fullName>Thrombin-activable fibrinolysis inhibitor</fullName>
<shortName>TAFI</shortName>
</alternativeName>
</protein>...other tags
</entry>
I though of using EvaluateXQuery processor and EvaluateXPath. EvaluateXQuery is using for getting accession and alternativeName tags. EvaluateXPath is using for getting name, recommendedName (fullName) tags.
Is it possible to use both and combine them to only xml or hust necessary to use EvaluateXQuery processor, like this:
<accessions>Q96IY4, A8K464, Q15114,...</accessions>
<name>CBPB2_HUMAN</name>
<recommendedName>Carboxypeptidase B2</recommendedName>
<alternativeNames>Carboxypeptidase U-CPU, Plasma carboxypeptidase B-pCPB,...</alternativeNames>
Can you help me, please?
Thank you!
Hi you may use processor execute groovy script and parse xml with a script to get your values and put them into attributes.

XSL - Externalizing Xpath queries to a property file

I went through various posts, regarding reading properties from external property files. Looks like there is a function - getProperty, which can read values from a property file, using a key. I am using saxon parser with spring integration. I am trying something like this, as described in the post :-
spring context file:
<int-xml:xslt-transformer id="xsltTransformer" input-channel="bulkStringInboundChannel"
output-channel="toBridgeChannel" result-type="StringResult" **transformer-factory-class="net.sf.saxon.TransformerFactoryImpl"**
xsl-resource="classpath:/META-INF/spring/integration/intake/intake-flow/bulkTransformer.xsl" />
XSL style sheet:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
....
<xsl:variable name="props" select="document('prop.xml')" />
<xsl:value-of select="f:getProperty('query1')"/>
....
Prop.xml:
query1 = /Batch/RequestID/text()
Error description:
XPST0003: XPath syntax error at char 23 on line 30 in {f:getProperty('query1')}:
XTSE0650: No template exists named getProperty
I now have two questions- first of all, how do I get rid of these errors?
Second, can I store xPath queries in property files? The post describes a method, to read a property file and use the value pertaining to its key. However, I am thinking that getProperty will just print the query's text equivalent instead of evaluating the query and processing it. Is there a way to achieve this?
Post - How to read a .properties file inside a .xsl file?
I can't help you with the Spring side of the question, but as for the Saxon side, you can call the JDK method System.getProperty() using code like this:
<xsl:value-of select="System:getProperty('user.dir')" xmlns:System="java:java.lang.System"/>
Java extensibility requires Saxon-PE or higher.
If the value of the property that you read is an XPath expression, you can then execute it using the XSLT 3.0 xsl:evaluate instruction - which also requires Saxon-PE or higher.

Schema Validation using Nokogiri

I am trying to validate an XML document against a dozen or so schemas using Nokogiri. Currently I have a root schema document that imports all the other schemas, and I validate against that.
Can I point to each schema file from the XML file itself, and have Nokogiri look in the XML file for the schemas to validate against?
The proper way to reference multiple schemata against which to validate an XML file is with the schemaLocation attribute:
<?xml version="1.0"?>
<foo xmlns="http://bar.com/foo"
xmlns:bz="http://biz.biz/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://bar.com/foo http://www.bar.com/schemas/foo.xsd
http://biz.biz/ http://biz.biz/xml/ns/bz.xsd">
For each namespace in your document you list a pair of whitespace-delimited values: the namespace URI followed by a 'hint' as to where to find the schema for that namespace. If you provide a full URI for each hint, then you can process this with Nokogiri as such:
require 'nokogiri'
require 'open-uri'
doc = Nokogiri.XML( my_xml )
schemata_by_ns = Hash[ doc.root['schemaLocation'].scan(/(\S+)\s+(\S+)/) ]
schemata_by_ns.each do |ns,xsd_uri|
xsd = Nokogiri::XML.Schema(open(xsd_uri))
xsd.validate(doc).each do |error|
puts error.message
end
end
Disclaimer: I have never attempted to validate a single XML document using multiple namespaced schemata with Nokogiri before. As such, I have no direct experience to guarantee that the above validation will work. The validation code is based solely on Nokogiri's schema validation documentation.

Resources