Sorting an XML file with Qt - c++

how to to sort an XML file using Qt
my file look like this :
<?xml version="1.0" encoding="UTF-8"?>
<project>
<task next="2" first="1" name="2" value="name1"/>
<task next="3" first="1" name="1" value="name2"/>
<task next="4" first="3" name="4" value="name3"/>
<task next="4" first="1" name="6" value="name4"/>
<task next="5" first="2" name="3" value="name5"/>
<task next="5" first="4" name="5" value="name6"/>
</project>
Thanks.

Basically, you need to parse the XML file into a set of records, sort the records on the appropriate field, then write the result back out as a new XML file. There are zillions of XML parsers out there that are intended to make it easy for you to parse the data. Personally, I've always written my own code to handle it. It's almost as fast to write, and executes quite a bit faster -- but that's me, and the XML I've parsed this was has mostly been fairly simple. If you lack experience at writing parsers and/or don't care as much about execution speed, chances are that using an existing parser will be a better choice.

What do you mean by sorting an xml file?
I think this needs a bit more thought.
Either sort the values after you read them from the file in your app or to work on the XML file directly
You may wish to look into xslt.

Related

Controlling JRebel package scope

I am trying to speed up execution of code being debugged with JRebel. In particular, I notice that framework code is slow. I am wondering whether I can tell JRebel to ignore certain packages, in much the same way that we can setup JProfiler to ignore certain packages and patterns.
You most definitely can.
Use a system property (or add to jrebel.properties) meant just for that purpose. More information at JRebel agent properties.
-Drebel.exclude_packages=PACKAGE1,PACKAGE2,...
Specify the excluded packages in rebel.xml using Ant-styled patterns. More information at rebel.xml configuration.
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zeroturnaround.com" xsi:schemaLocation="http://www.zeroturnaround.com http://update.zeroturnaround.com/jrebel/rebel-2_1.xsd">
<classpath>
<dir name="/path/to/module/build/directory/root">
<exclude name="com/yourapp/package1/internal/**"/>
</dir>
</classpath>
</application>
Both ways work similarly but since the second one enables to customize each module inividually it is generally preferred.

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 .

Multiple XSLT files in a single pipeline with ant

I have multiple XSLT files that I'm using to process my source XML in a pipeline. I know about the trick with exsl:node-set but after having some issues with this workflow, I took the decision to split the various passes into separate XSL files. I'm much happier with the structure of the files now and the workflow works fine in Eclipse. Our release system works with ant. I can process the files like this:
<xslt basedir="src-xml" style="src-xml/preprocess_1.xsl" in="src-xml/original.xml" out="src-xml/temp_1.xml" />
<xslt basedir="src-xml" style="src-xml/preprocess_2.xsl" in="src-xml/temp_1.xml" out="src-xml/temp_2.xml" />
<xslt basedir="src-xml" style="src-xml/preprocess_3.xsl" in="src-xml/temp_2.xml" out="src-xml/temp_3.xml" />
<xslt basedir="src-xml" style="src-xml/finaloutput.xsl" in="src-xml/temp_3.xml" out="${finaloutput}" />
But this method, going via multiple files on disk, seems inefficient. Is there a better way of doing this with ant?
Update following Dimitre's suggestion
I've created myself a wrapper around the various other XSLs, as follows:
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:fn='http://www.w3.org/2005/xpath-functions' xmlns:exslt="http://exslt.org/common">
<xsl:import href="preprocess_1.xsl"/>
<xsl:import href="preprocess_2.xsl"/>
<xsl:import href="preprocess_3.xsl"/>
<xsl:import href="finaloutput.xsl"/>
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-imports />
</xsl:template>
</xsl:stylesheet>
This has... not worked well. It looks like the document had not been preprocessed before the final output XSL ran. I should perhaps have been clearer here: the preprocess XSL files are modifying the document, adding attributes and the like. preprocess_3 is based on the output of ..._2 is based on ..._1. Is this import solution still appropriate? If so, what am I missing?
The more efficient method is to perform a single, multipass transformation.
The files can remain as they are -- they will be imported using xsl:import instructions.
The savings are obvious:
Just one initiation (loading of the XSLT processor).
Just one termination.
Eliminates the two intermediate files and their creation, writing into, closing and deleting.
Hmm, you say I know about the trick with exsl:node-set, but you don't use it in your attempt ("Update following Dimitre's suggestion"). In case you don't know it, or for the others (like me) who don't know how to perform multipass transformation, here is a nice article: Multipass processing.
The drawback of this approach is that it requires engine specific xsl code. So if you know the engine, you could try this. If you don't know the engine, you could try with solutions from result tree fragment to node-set: generic approach for all xsl engines.
Looking at these sources one conclusion is sure: your current solution is more readable. But you are seeking efficiency, so some readability may be sacrificed.

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.