XML Parser - Data Binding C++ Builder - c++

I'm a bit stuck and new to C++. Basically I have an XML Document like this:
<Program>
<DOSCommands>
<Command>ipconfig /all</Command>
</DOSCommands>
<DOSCommands>
<Command>chkdsk</Command>
</DOSCommands>
<Copy>
<File>lol.txt</File>
</Copy>
<Copy>
<File>BestMan.txt</File>
</Copy>
</program>
Obviously my real XML contains lots of Copy and Dos tags. I have generated the code for this from the XML data binding.
I can not seem to load these into my data structure properly. Can anyone point me in the right direction?

Unless this is a homework assignment, why not use a library like tinyxml2 and don't reinvent the wheel? It has a very nice c++ interface.

Related

Getting generated xml from server control

I don't have much experience in server controls and xslt. I think I'm missing something fundamental and it seems that I can't find the right keywords to find what I need...
This works fine, it was not written by me. I'm just trying to understand it:
<CMS:FlexMenu ID="flexmenu1" runat="server" CssClass="usersClass" DefaultMenuID="83"
WrapTag="div" AutoCollapseBranches="True" StartCollapsed="True" EnableMouseOverPopUp="False"
EnableSmartOpen="True" StartLevel="1" MenuDepth="0" EnableAjax="False" DisplayXslt="/xmlfiles/flexmenu-to-list.xsl" />
This is an Ektron server control. If I needed to change or write a new xslt file, where can I find the structure of the XML?
I'm aware that you can write up an xslt that will spit out the raw xml. But my intuition tells me there has to be an easier way. What am I missing?
You can tell the FlexMenu control to write out the raw XML.
flexmenu1.DataBind();
Response.Write(flexmenu1.Text);
Response.End();

Modify XML File in C++

I want to modify an XML file which has a very complicated structure something like
<root>
<a></a>
<b></b>
<c></c>
<d>
<e>text</e>
<f>text</f>
<d>
<d>
....
</root>
I tried to use tinyxml and it is good but there are some comments in the source xml file that I want to keep so I thought may be dealing with the file as mere text can be a good idea, however the string functions in c++ are crippling me because I'll have to search and replace, any ideas?
Note:There are no attributes in the XML, just values between the tags.
I don't know if using a third party library is off the table for parsing but I've been using this XSD parser which allows you to serialize XML files to memory. You are then free to edit and/or read the values at their node locations and then write the file back out.
This, to me, at least, is significantly simpler than generating a custom parser for each type of XML input.

Find values in char array c++

I have a char array and the text actually looks like XML:
<root>
<number>1</number>
<counter>2</counter>
<lastNumber>3</lastNumber>
</root>
I need to get these values and store them to variables so I can work with them.
Any idea how to do that?
Thank you
You could use TinyXML, to parse the file. http://www.grinninglizard.com/tinyxml/
Its a open source library to parse XML files in C++.
You should formulate a better question, but the general idea is clear and so I suggest to use a library such as boost::spirit or, if you can arrange your data in a different way, you can use boost::config which is probably a much simpler approach and here is an example .

Parsing a xml File and find any errors

I have an configuration xml file which has some values like
<config>
<map>100,1,200,1</map>
<image>abc.bmp</image>
.
.
.
.
</config>
etc.
I imported the file read line by line all are done. I have to validate the fields in file. Like
1. <map> " "</map> is not empty,no junk value,
2. <image>abc**,**bmp</im*E*ge> (spelling mistake)
3. <image>abc.bmp </config> ( missing tags)
I have to develop a unique algorithm so that cant use libs . Is there any idea rather than loading and checking every one character by character?
I'd recommend to use a 3rd party library to implement XML parsing. Getting all the details and pitfalls of XML parsing correct is much harder than you might think.
Your points 2. and 3. will be supported well by any complete XML parser. Point 1. will need either XSL schema definition and a parser that supports schema validation, or you'll need to provide extra validation code manually.
If you're concerned about impact (code/memory usage) you should refer to these lightweight C/C++ XML parsers:
C++ expat (can be used in commercial projects)
TinyXml (can be used in commercial projects)
Other XML parsers
POCO XML
Xerces C++ (provides XSL Schema validation)

c++ linux library for creating an xml and reading from an xml (serialize/ deserialize)

I am working in Ubuntu. I have a .h file with a class and a lot of nested classes. I would like to create an XML file from an object. Can someone please give me a library that creates XML files, serializes, and deserializes objects? I am compiling with g++.
Try libxml2.
But it seems like you want to serialize and desirialize an object from and to XML. Boost::serialization might come in handy. it also supports serialization from and to XML.
Here you can find an example for Boost::serialization with XML.
If you want to handle XML in C++ you may have a look at these projects
http://xmlsoft.org/
http://www.grinninglizard.com/tinyxml/
http://xerces.apache.org/xerces-c/
It doesn't serialize with XML (which I consider a feature, personally), but Google protocol buffers does a good job of serializing (in a binary format) objects that are defined in the .proto language.
You may want to explore the XML Data Binding. The main idea is that given an xml schema the data binding software generates a class hierarchy corresponding to the schema, and the code to serialize / unserialize (called marshal / unmarshal). There are several tools that can do this, gsoap is a free one, xmlSpy is one of the commercial ones.
What you describe is an XML data binding for C++. There are several tools for what you want to do, see e.g. XML Data Binding Tools. I've used gSOAP for several C++ projects, including starting from C++ files with classes which is really nice (other tools force you to start from XML schemas or WSDLs). With gSOAP I have been able to generate XML schemas and XML, see e.g. map C/C++ types to XML schema.
A super-lightweight, simple xml library is pugixml.
Though keep in mind that C++ does not have the reflection capabilities that .NET has. No library will generate the serialization/deserialization code for you (which I guess you hoped for).