Find values in char array c++ - 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 .

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.

XML Parser - Data Binding C++ Builder

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.

How to start using xml with C++

(Not sure if this should be CW or not, you're welcome to comment if you think it should be).
At my workplace, we have many many different file formats for all kinds of purposes. Most, if not all, of these file formats are just written in plain text, with no consistency. I'm only a student working part-time, and I have no experience with using xml in production, but it seems to me that using xml would improve productivity, as we often need to parse, check and compare these outputs.
So my questions are: given that I can only control one small application and its output (only - the inputs are formats that are used in other applications as well), is it worth trying to change the output to be xml-based? If so, what are the best known ways to do that in C++ (i.e., xml parsers/writers, etc.)? Also, should I also provide a plain-text output to make it easy for the users (which are also programmers) to get used to xml? Should I provide a script to translate xml-plaintext? What are your experiences with this subject?
Thanks.
Don't just use XML because it's XML.
Use XML because:
other applications (that only accept XML) are going to read your output
you have an hierarchical data structure that lends itself perfectly for XML
you want to transform the data to other formats using XSL (e.g. to HTML)
EDIT:
A nice personal experience:
Customer: your application MUST be able to read XML.
Me: Er, OK, I will adapt my application so it can read XML.
Same customer (a few days later): your application MUST be able to read fixed width files, because we just realized our mainframe cannot generate XML.
Amir, to parse an XML you can use TinyXML which is incredibly easy to use and start with. Check its documentation for a quick brief, and read carefully the "what it does not do" clause. Been using it for reading and all I can say is that this tiny library does the job, very well.
As for writing - if your XML files aren't complex you might build them manually with a string object. "Aren't complex" for me means that you're only going to store text at most.
For more complex XML reading/writing you better check Xerces which is heavier than TinyXML. I haven't used it yet I've seen it in production and it does deliver it.
You can try using the boost::property_tree class.
http://www.boost.org/doc/libs/1_43_0/doc/html/property_tree.html
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_propertytree/tutorial.html
http://www.boost.org/doc/libs/1_43_0/doc/html/boost_propertytree/parsers.html#boost_propertytree.parsers.xml_parser
It's pretty easy to use, but the page does warn that it doesn't support the XML format completely. If you do use this though, it gives you the freedom to easily use XML, INI, JSON, or INFO files without changing more than just the read_xml line.
If you want that ability though, you should avoid xml attributes. To use an attribute, you have to look at the key , which won't transfer between filetypes (although you can manually create your own subnodes).
Although using TinyXML is probably better. I've seen it used before in a couple of projects I've worked on, but don't have any experience with it.
Another approach to handling XML in your application is to use a data binding tool, such as CodeSynthesis XSD. Such a tool will generate C++ classes that hide all the gory details of parsing/serializing XML -- all that you see are objects corresponding to your XML vocabulary and functions that you can call to get/set the data, for example:
Person p = person ("person.xml");
cout << p.name ();
p.name ("John");
p.age (30);
ofstream ofs ("person.xml");
person (ofs, p);
Here's what previous SO threads have said on the topic. Please add others you know of that are relevant:
What is the best open XML parser for C++?
What is XML good for and when should i be using it?
What are good alternative data formats to XML?
BTW, before you decide on an XML parser, you may want to make sure that it will actually be able to parse all XML documents instead of just the "simple" ones, as discussed in this article:
Are you using a real XML parser?

what is the best way to write xslt -fo out of xsl+xml

i have complex xslt that formats xml to html
now i need to be able to create xsl fo out of it
what is the best way to do it ?
Here is some ineresting article for you
http://www-128.ibm.com/developerworks/library/x-xslfo2app/
Also you can try next library (I dont't remember, probably it can creat fo files from xslt+xml):
http://www.codeproject.com/KB/dotnet/nfop.aspx
I had a similar requirement, and I did some research, but didn't find a reliable XHTML to FO transformer. There may be one, but there comes a point in some web searches when you have to give up and roll your own.
Instead I took the XML to HTML transformer I had already written and changed it to output FO.
This is a much simpler proposition that a full blown XHTML to FO transformer.
Whilst your details will differ, most structures in HTML have analogues in FO, so you can normally decide what FO construct to use in replacing HTML in your transform fairly easily.
I did this incrementally. If you start with the transform producing the outline of an FO document, and most of your XML being ignored in the transform, you can then build up the output in a measured fashion.