Creating IXMLDOMnode from string - c++

I have a string which contains the XML representation of an XML node which I intend to insert in a XML document loaded in memory. The XML string (of node)is something like this:
<ns1:Feature name=\"PageSize\">\
<ns1:Option name=\"A4\" />\
</ns1:Feature>
So, it has got namespace for the tag names as well.
Is there a way I can achieve this?
I tried to user XMLDomNode->put_text(), but it does not work as it replaces the "<" and ">" chars by their text representations (< etc.)
I was wondering if loading the string buffer in a separate in-memory XML document and then getting the node pointer from there will work on my original document. But again, not sure if the XMLDOMnodes are transferable within documents.

I solved this myself using the 2nd approach:
1) Create an in-memory xml document based on IXMLDOMDocument3 interface and load the xml string in there.
2) Select the node you require using the selectNode() method.
3) Now go back to your orinial xml document where you want the node placed and load it again as a IXMLDOMDocument3 interface.
4) Use the importNode() method of IXMLDOMDocument3 from step 3 to clone the node obtained in step 2.
5) You can now use the cloned node to do an appendChild() to the original xml.

Related

Select certain fields/children of flat xml with xpath

I have a xml file like a single database table, is there a way I can get "listpos"-rows of certain fields, ie. only "type" and "objName" ?
<listpos lfdNr="0001" reihe="20140626143443">
<type>Akt</type>
<objName>2#25.6.2014#40801#de</objName>
<laborOrt>au</laborOrt>
...
</listpos>
<listpos lfdNr="0002" reihe="20140626181936">
<type>Akt</type>
<objName>2#25.6.2014#40802#de</objName>
<laborOrt>au</laborOrt>
...
</listpos>
...
So the output should be
<listpos>
<type>Akt</type>
<objName>2#25.6.2014#40801#de</objName>
</listpos>
<listpos>
<type>Akt</type>
<objName>2#25.6.2014#40802#de</objName>
</listpos>
...
Writing this it comes to my mind it might be a job for xslt?
The whole thing I do, is trying to make django-xml work together with django_tables2 ...
Yes, by using XPath you can get that data pretty easily.
You navigate to the appropriate elements using Expressions.
For the data you're trying to extract (assuming you wanted to access all "type" and "objName" Elements), those would be "//listpos/type" and "//listpos/objName"
Those would get you the right nodes from which you can extract the content.

Boost ptree top level array

I would like to have write_json output a top level array, something to the effect of:
[{...},{...},{...},...,{...}]
But when I pass a list to write_json, it converts to a json full of blank keys.
{"":{...},"":{...},"":{...},..."":{...}}
Using add_child actually respects the array and gives me the closest thing of:
{"Some Key":[{...},{...},{...},...,{...}]}
But that's still not what I want.
Any idea how to make that array top level?
Boost does not have a JSON library (nor does it have an XML library). It has a Property Tree library (which happens to include a JSON compatible representation).
The limitations you run into are perfectly clearly documented right there: http://www.boost.org/doc/libs/1_62_0/doc/html/property_tree/parsers.html#property_tree.parsers.json_parser
The property tree dataset is not typed, and does not support arrays as such. Thus, the following JSON / property tree mapping is used:
JSON objects are mapped to nodes. Each property is a child node.
JSON arrays are mapped to nodes. Each element is a child node with an empty name. If a node has both named and unnamed child nodes, it cannot be mapped to a JSON representation.
JSON values are mapped to nodes containing the value. However, all type information is lost; numbers, as well as the literals "null", "true" and "false" are simply mapped to their string form.
Property tree nodes containing both child nodes and data cannot be mapped.
JSON round-trips, except for the type information loss.
It goes on to show an example of EXACTLY what you run into.

MSXMLDomDocument to consume XML

I have an existing MSXML2::DOMDocument. Into that Document I want to insert further XML from text.
What I have tried is creating a second DOMDocument and using its loadXML method. Then I append its documentElemt to my main XmlDocument.
This gives me a memory leak. I guess shifting Nodes from one Doc to another is not valid.
So my question is: what is the intended way to insert XMLSource (text) into an existing DomDocument?

Using XSLT how do we render CDATA tag?

Using XSLT how do we render CDATA tag?
In xslt I dont want to create CDATA tag using text or declaring in xml
output tag using cdata-section-elements,
it should read it dynamically from input, if element value is around CDATA than
then xslt should render the same, as shown below
Input:
<A><![CDATA[Hello World]]></A>
XSLT Output :
<A><![CDATA[Hello World]]></A>
The data model XSLT/XPath/XQuery operate on does not know any CDATA sections so you can't simply preserve them as the tree you operate on simply contains a text node in both cases (i.e. for <foo>a & b</foo> and <foo><![CDATA[a & b]]></foo> the tree is a foo element containing a single text child node with the string value a & b).
So there is no way in pure XSLT to achieve what you want, unless you pre-process the input to convert CDATA sections into some structure like elements the XSLT data model allows you to detect and distinguish. Andrew Welch has http://andrewjwelch.com/lexev/ to do that in a Java environment.
Thus if you use an XSLT 2.0 processor like Saxon 9 with Java you could use that approach.

Xpath return node relative to another node

I have done a search for all nodes that have an attribute containing (substring) a String. These nodes can be found at different levels of the tree, sometimes 5 or 6 levels deep. I'd like to know what parent/ancestor node they correspond to at a specified level, 2 levels deep. The result for the search only should be much greater than the results for the corresponding parents.
EDIT to include code:
/xs:schema/xs:element/descendant::node()/#*[starts-with(., 'my-search-string-here')]
EDIT to clarify my intent:
When I execute the Xpath above sometimes the results are
/xs:schema/xs:element/xs:complexType/xs:attribute or
/xs:schema/xs:element/xs:complexType/xs:sequence/xs:element or
/xs:schema/xs:element/xs:complexType/xs:complexContent/xs:extension/xs:sequence/xs:element
These results indicate a place in the Schema where I have added application specific code. However, I need to remove this code now. I'm building an "adapter" schema that will redefine the original Schema (untouched) and import my schema. The String I am searching for is my prefix. What I need is the #name of the /xs:schema/node() in which the prefix is found, so I can create a new schema defining these elements. They will be imported into the adapter and redefine another schema (that I'm not supposed to modify).
To reiterate, I need to search all the attributes (descendants of /xs:schema/xs:element) for a prefix, and then get the corresponding /xs:schema/xs:element/#name for each of the matches to the search.
To reiterate, I need to search all the attributes (descendants of /xs:schema/xs:element) for a prefix, and then get the corresponding /xs:schema/xs:element/#name for each of the matches to the search.
/
xs:schema/
xs:element
[descendant::*/#*[starts-with(., 'my-search-string-here')]]/
#name
This should do it:
/xs:schema/xs:element[starts-with(descendant::node()/#*, 'my-search-string-here')]
You want to think of it as
select the xs:elements which contain a node with a matching attribute
rather than
select the matching attributes of descendant nodes of xs:elements, then work back up
As Eric mentioned, I need to change my thought process to select the xs:elements which contain a node with a matching attribute rather than select the matching attributes of descendant nodes of xs:elements, then work back up. This is critical. However, the code sample he posted to select the attributes does not work, we need to use another solution.
Here is the code that works to select an element that contains and attribute containing* (substring) a string.
/xs:schema/child::node()[descendant::node()/#*[starts-with(., 'my-prefix-here')]]