libxml - Load xmlDoc from raw data - c++

I have a char* buffer of data that I want to parse as XML in libxml2.
How would one go about that?
Currently I am using it to open the file automatically by calling a file name, but it would be nice to have more functionality.
Currently I do it like this:
xmlDocPtr doc = xmlParseFile("data/foo.xml");
However I have a resource system that gives me access to the raw data, so my more preferred method would be:
resource_base_ptr res = load_resource("data/foo.xml");
xmlDocPtr doc /*= some_function(res->raw_data) */;

You need to use xmlReadMemory()
http://xmlsoft.org/html/libxml-parser.html#xmlReadMemory

Related

how to create a JSON file with c++ methods

We are doing a black box testing in our project,For that we wanted to use json and c++ and Jzon for parsing.can anybody show how to write a json file for c++ method and parse to get the node by using jzon ?
To write Json object (Jzon::Node) to file you can use Jzon::Writer class.
There are three methods writeStream,writeString and writeFile. Based on target where you want to save your object you can you appropriate method. To save into file you should use writeFile.
You code should be like this.
std::string filename="some path and file name.json";
Jzon::Node node;
//fill your object
Jzon::Writer write;
write.writeFile(node,filename);

Open XML SDK - Create docx from template (dotx) using MemoryStream as docx holder

I am trying to create a .docx file from a word template (.dotx), using OpenXML 2.5 library. More exactly, I'm trying to follow this , but I want to open the document using a MemoryStream and not the path to the file.
So in the link above, the approach is this: Copy the template file in a new file on disk. Open the new file as a WordProcessingDocument, change its type to Document and add a reference to the template (I'm not sure of why is this necessary ...)
What I need is. Get my template from the DB as byte array. Copy the array to a stream and open it as a WordProcessingDocument. Then do the same thing as above.
I don't want to use temporary files on disk and so on. What I don;t understand is why should I reference the template file in the new document (Actually, inspecting the xml files in the docx archive, it just adds a xml node containing the name of the template... why?)
Now, my problem is that if I use a MemoryStream and not a physical path to the file, the resulting docx is corrupted. I'm getting "Microsoft office cannot open this file because some parts are missing or invalid".
If I use temporary disk files, it works.
Why is this happening?
Simplified, my code looks like this:
byte[] result = null;
byte[] templateBytes = System.IO.File.ReadAllBytes("template.dotx");
using (MemoryStream templateStream = new MemoryStream())
{
templateStream.Write(templateBytes, 0, (int)templateBytes.Length);
using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true))
{
doc.ChangeDocumentType(WordprocessingDocumentType.Document);
var mainPart = doc.MainDocumentPart;
//Here I should create an AttachedTemplate object and assign a relationship id to it, which also implies
//setting a "reference" to the template ? I'm not using files so there's nothing to "reference" anyway
// If it's doing more than just putting a visual reference to the template file, then why limit the way of referencing the template
// only by using the Uri to the file? Please take a look at the above link to see what I'm referring to.
//Write some text in the file ....
mainPart.Document.Save();
File.WriteAllBytes("Result.docx", templateStream.ToArray());
}
}
This gets me a corrupted docx file. If I switch from MemoryStream to opening the new docx file from disk (using the other constructor of the WordProcessingDocument class), it works.
I'm not doing something wrong (like trying to write the result to the file after disposing the memory stream), so it should give me the same result.
What is wrong with my code?
Thanks
If you take the last line of your code File.WriteAllBytes out of the inner using block, it should work correctly. Just put that line right before the closing brace of the outer using block.

qt registerresource from raw data

I am just starting to learn about the QResource. As I understand it, it is just a way to create a "directory/File" structure in memory.
I'd like my application to create a resource (at run time) from a raw data. E.g. my application could create a image resulting from some computation and would want to save it as a resource for further usage.
I thought that the constructor:
QResource::registerResource( const uchar * rccData, const QString & mapRoot = QString()
was meant to do that, but I probably missed something as I expected a size argument...
what is rccData then?
How big the the created resource?
How can I create a resource from raw data?
Thanks in advance for your answers.
QResource gives you access to data stored in resource files .qrc. These files inherit a directorylike structure, which then can be accessed through the QResource class. These files can be compiled into the application or can be loaded at runtime via QResource::QResource ( const QString & file = QString(), const QLocale & locale = QLocale() ) consructutor. Those loaded files then virtually repesent a directory structure in memory, but QResource is not meant to create a directory structure out of nothing.

XercesC setting output to UTF-8

I'm using XercesC Lib to create a serialization of my data. How can I set it to UTF-8? It is always generated with UTF-16 and I can't find a way to change that.
xercesc::DOMImplementation *gRegistry = xercesc::DOMImplementationRegistry::getDOMImplementation(X("Core"));
xercesc::DOMDocument *doc = gRegistry->createDocument(
0, // root element namespace URI.
X(oDocumentName.c_str()), // root element name
0); // document type object (DTD).
doc->setXmlStandalone(true);
... prepare the document ...
serializer = ((xercesc::DOMImplementationLS *)gRegistry)->createLSSerializer();
serializer->setNewLine(xercesc::XMLString::transcode("\n"));
XMLCh *xmlresult = serializer->writeToString(doc);
char *temp = xercesc::XMLString::transcode(xmlresult);
std::string result(temp);
xercesc::XMLString::release(&temp);
xercesc::XMLString::release(&xmlresult);
doc->release();
serializer->release();
getStream() << result.c_str();
When I deserialize with JAXB on the Java side, I always get a content is not allowed in prolog and so far this is the only difference I can see in the XML. When I try to locally deserialze in JAXB it works. When I take my XercesC XML I get this error. When I try to format it in Notepad++ with the XML plugin it also says that there is an error, but doesn't tell me any details.
Check the usage of DOMLSOutput, that should give you exactly what you want. I.e. you create a DOMLSOutput object to which you write (instead of using DOMLSSerializer::writeToString).

Use a MSHFlexGrid with non-database data and still define the shape

I am trying to use the Microsoft Hierarchical FlexGrid (MSHFlexGrid) in a Visual C++ (VS 2005). I have the grid shown, and I can manually add data to the individual cells. However, according to online documentation I've read, I should be able to show the hierarchical nature of the data (hence MSHFlexGrid instead of MSFlexGrid) by defining the SHAPE as the RecordSource. I can do that fine (by using the put_RecordSource method of the grid object), however I'm at a loss as to how to add the actual data.
I've read that the best way to do this is to use an ADO Data Control (ie ADODC) component and bind it as the DataSource for the Grid. You can then specify "provider=msdatashape;data provider=none;" as the provider of the DataControl and fill it with data. If I were doing SQL, I'd specify my SELECT query as the RecordSource, then call Refresh() and let the control load the data.
However, my data is in custom objects. I know what needs to be displayed, I'm just at a loss as to the best way to insert the data into the FlexGrid and still use the built in features of the control. I'm open to any suggestions, but I need to keep the data local (ie no JET, Access, etc).
Here's some code:
In header:
....
// Variable to control the Flex Grid component
CMshflexgrid1 m_grid; //generated by wizard from the MSHFlexGrid component
// to control the data source hierarchical information
CAdodc1 m_adodc1;
....
In cpp:
....
BOOL MyDialogClass::OnInitDialog()
{
CDialog::OnInitDialog();
m_grid.Clear();
CString strCn = "provider=msdatashape;data provider=none;";
m_adodc1.put_ConnectionString(strCn);
CString BackupOfRecordSource = "";
BackupOfRecordSource = m_adodc1.get_RecordSource();
//CString strShape = "SHAPE APPEND new adInteger As PID, New adVarChar(10) As StudentName, ((SHAPE APPEND new adInteger As ChID, New adVarChar(10) As Course, ((SHAPE APPEND new adInteger As GrndChID, New adBSTR As Description) RELATE ChID TO GrndChID) As GrandChild) RELATE PID TO ChID) AS Child";
CString strShape = "SHAPE APPEND new adInteger As PID, New adVarChar(10) As StudentName";
m_adodc1.put_RecordSource(strShape);
m_adodc1.Refresh();
m_grid.Refresh();
BackupOfRecordSource = m_adodc1.get_RecordSource(); //returns the strShape that I just put in
//ADD RECORDS HERE! HOW?
return TRUE;
}
The sample talked about building an ADODB.Recordset and use it as the data source of ADODC. The code you give is building a SQL and use it as the data source of ADODC. I don't think you can replace an ADODB.Recordset with a string.