I need to write a XML Parser using Boost Property tree which can replace an existing MSXML DOM Parser. Basically my code should return the list of child nodes, number of child nodes etc. Can this be achieved using Property Tree? Eg. GetfirstChild(),selectNodes(),Getlength()etc.
I saw a lot of APIs related to Boost Property Tree, but the documentation seems to be bare minimum and confusing. As of now, I am able to parse the entire XML using BOOST_FOREACH. But the path to each node is hard coded which will not serve my purpose.
boost::property_tree can be used to parse XML and it's a tree so you can use as XML DOM substitution but the library is not intended to be fully fledged XML parser and it's not complaint with XML standard. For instance it can successfully parse non-wellformed xml input and it doesn't support some of XML features. So it's your choice - if you want simple interface to simple XML configuration then yes, you should use boost::property_tree
Related
I want to use FreeMarker to process an XML template. However, instead of letting FreeMarker write the output to a Writer, I’d like to traverse the processed DOM tree. (Essentially, I want to fill a Protobuf3 structure from it.)
Is that possible?
FreeMarker is not imposing any kind of DOM-like structure on the template — unlike Thymeleaf.
So, it is not possible to automatically get a DOM, but the output has to be parsed into a DOM afterwards.
I am trying to write and to read/parse MathMl content XML files with boost ptree (property_tree)I cannot seep. I cannot solve to write or to parse this code:
<?xml version="1.0" encoding="utf-8"?>
<math xmlns="http://www.w3.org/1998/Math/MathML">
<cn type="rational">1<sep/>2</cn>
</math>
The problem is the "sep/". When I use get_value() or get() with string or int I get "12". I cannot separate the 1 and the 2. How can I get or write the two separate values "1" and "2".
Boost Property Tree is not an XML parser.
Instead, it's a settings persistency utility, that facilitates to
(de) serialize a certain set of hierarchical data types
to a number of (partly interchangeable) formats
Note that the featureset for each format is not the same.
Specifically for your goal you need a parser that handles mixed-content elements (elements containing both text and sub-elements, mixed). There's a surprising number of XML parsers that don't handle this. Boost Property Tree is (uses?) such a parser.
So, you should look at another library to get you this.
What XML parser should I use in C++?
Ox and similar libraries provide the serialisation of ruby objects to XML, but are there any libraries which allow you to define the form that serialisation takes?
Essentially, is there a library akin to (haml|slim|mustache) which allows you to define the mapping of a hash (lets say) to an XML document, but which can also parse the XML document and generate the original hash?
(assuming all the elements of the hash are mapped)
If the mapping is not too exotic, you might be able to do it with representable. It also supports JSON, by the way.
What's a good way to transform XML to ruby code? I've got a GraphML file containing information about a graph structure. I want to instantiate a graph from that with ruby objects.
Currently I use XPath to do this in a procedural way. I know, there's also a way to do it with XSLT in a more declarative way.
Do you know other ways? What would you suggest, any experience?
I don't quite understand why you would want to transform the GraphML data into Ruby code, rather than using Ruby to parse the GraphML data into Ruby object instances?
I made this example as an exercise: https://github.com/endymion/GraphML-parsing-exercise
It uses Nokogiri to parse the XML, then XPath to select nodes, then it iterates through the nodes, instantiating Ruby object instances: https://github.com/endymion/GraphML-parsing-exercise/blob/master/parse.rb
Is that roughly what you're looking for?
Is it possible to update an rdf file dynamically from user generated input through a webform? The exact scenario would beskos concept definitions being created and updated through user input to html forms.
I was considering xpath but is there a better / generally accepted / best practice way of doing this kind of thing?
For this type of thing there are IMO two approaches:
1 - Using Named Graphs in a Triple Store
Rather than editing an actual fixed file you use a Graph which is stored as a named graph in a Triple Store that supports triple level updates (i.e. you can change individual Triples in a Graph). For example you could use a store like Virtuoso or a Jena based store (Jena SDB/TDB) to do this, basically any store that supports the SPARUL language or has it's own equivalent.
2 - Using a fixed RDF file and altering it
From your mention of XPath I assume that you are intending to store your file as RDF/XML. While XPath would potentially work for this it's going to be dependent on the exact serialization of your file and may get very complex. If your app is going to allow users to submit and edit their own files then they'll be no guarantees over how the RDF has been serialized into RDF/XML so your XPath expressions might not work. If you control all the serialization and processing of the RDF/XML then you can keep it in a format that your XPath will work on.
From my point of view the simplest way to do this approach is to load the file into memory using an appropriate RDF library, manipulate it in memory and then persist the whole thing back to disk when the user is done (or at regular intervals or whatever is appropriate to your application)