I'm using TinyXml2 v8.0.0 to create an XML buffer to send to an API. The example includes a declaration. I'm implementing this with:
XMLDocument doc;
doc.InsertEndChild(doc.NewDeclaration());
XMLElement* pRoot = doc.NewElement("Stuff");
doc.InsertFirstChild(pRoot);
The documentation for NewDeclaration states:
If the text param is null, the standard declaration is used.:
<?xml version="1.0" encoding="UTF-8"?>
You can see this as a test in https://github.com/leethomason/tinyxml2/blob/master/xmltest.cpp#L1637
But when I print the buffer out the declaration has been placed at the end of the buffer after a newline:
<Stuff>
</Stuff>
<?xml version="1.0" encoding="UTF-8"?>
Does anyone know why this is happening? I'd expect it to be at the start of the buffer with no newline.
Presumably, that's because you told it to put the declaration as the EndChild and the Stuff element as the FirstChild.
Related
I'm using xerces-c to write XML. By default it writes the entire DOM as a single line of text. I tried the pretty-print option, like below, and now it prints double-spaced lines - which isn't very pretty, in my opinion. Is there a way to avoid the double-spacing?
void configureWriter(DOMLSSerializer* writer) {
writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
}
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<MyDocument>
<A>B</A>
<D>E</D>
</MyDocument>
OK, I found an answer. There's a different option called "fgDOMWRTXercesPrettyPrint", and if you also turn this off then there are no empty lines in the output.
void configureWriter(DOMLSSerializer* writer) {
writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
writer->getDomConfig()->setParameter(XMLUni::fgDOMWRTXercesPrettyPrint, false);
}
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<MyDocument>
<A>B</A>
<D>E</D>
</MyDocument>
This was the mail thread which gave me the answer: http://mail-archives.apache.org/mod_mbox//xerces-c-users/200908.mbox/%3C4A7697C0.1000304#datadirect.com%3E
How to "pretty print" an XML string in C++ using Qt or boost?
I have an input like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Something property1="A" property2="B"><Value>42</Value></Something>
an I want to receive this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Something property1="A" property2="B">
<Value>42</Value>
</Something>
Sounds like a very simple task, but I have difficulties with solving it properly using Qt or boost.
When I try to use a QDomDocument
QDomDocument dom_document;
dom_document.setContent(unformatted_xml);
QString formatted_xml = dom_document.toString(4);
it both messes with the quotes (https://forum.qt.io/topic/68098/issue-with-qdomdocument-setcontent-and-xml-declaration-attribute-quotes) and swaps some properties (e.g., property2 might end up being before property1)
When I try to use a boost::property_tree
using boost::property_tree::ptree;
ptree pt;
std::istringstream iss{i_unformatted_xml};
read_xml(iss, pt, (boost::property_tree::xml_parser::trim_whitespace | boost::property_tree::xml_parser::no_concat_text));
boost::property_tree::xml_writer_settings<std::string> settings{' ', 4};
std::ostringstream result_oss;
write_xml(result_oss, pt, settings);
it messes with the header, removing its standalone property completely and transforming "UTF-8" to lowercase.
Is there a way to just format an XML string without modifying any non-whitespace characters in it?
E.g., like the XML Tools plugin for Notepad++ does.
You can try QXmlFormatter, another way of formatting XML in Qt. At least from the documentation its purpose seems to be to just insert linebreaks and space characters, not modifying the XML in other ways. (I didn't verify in a test, though.)
QXmlFormatter works on QXmlQuery output, so you'd have to use a fake XQuery that does not change its input.
First of all i would like to big thanks to everyone in this forum.
I was trying to extract one xml tag value in the below given xml string.
Input String was :-
<?xml version="1.0" encoding="UTF-8"?>
<nonpublicExecutionReportAcknowledgement xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/FpML-5/recordkeeping /../xmls/SDR/recordkeeping/fpml-main-5-5.xsd">
<header>
<inReplyTo messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</inReplyTo>
<sentBy>DTCCEU</sentBy>
<sendTo>RRasdfjasdfasdkllkd4</sendTo>
<creationTimestamp>2015-10-14T16:47:30Z</creationTimestamp>
</header>
<originalMessage>
<nonpublicExecutionReport fpmlVersion="5-5" xmlns="http://www.fpml.org/FpML-5/recordkeeping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<messageId messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</messageId>
<sentBy messageAddressScheme="http://www.fpml.org/coding-scheme/external/cftc/interim-compliant-identifier">RRasdfjasdfasdkllkd4</sentBy>
<sendTo>DTCCEU</sendTo>
<creationTimestamp>2015-10-14T05:54:38Z</creationTimestamp>
</header>
</nonpublicExecutionReport>
</originalMessage>
</nonpublicExecutionReportAcknowledgement>
Regex i was used to extract messageId was "(?<=>).*?.(?=\</messageId)" which is working fine. But when it comes as single xml string it was not working as expected.
Failing for the below input string.
<?xml version="1.0" encoding="UTF-8"?><nonpublicExecutionReportAcknowledgement xmlns="http://www.fpml.org/FpML-5/recordkeeping" fpmlVersion="5-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.fpml.org/FpML-5/recordkeeping /../xmls/SDR/recordkeeping/fpml-main-5-5.xsd"><header><inReplyTo messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</inReplyTo><sentBy>DTCCEU</sentBy><sendTo>RRasdfjasdfasdkllkd4</sendTo> <creationTimestamp>2015-10-14T16:47:30Z</creationTimestamp></header><originalMessage><nonpublicExecutionReport fpmlVersion="5-5" xmlns="http://www.fpml.org/FpML-5/recordkeeping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><header> <messageId messageIdScheme="www.abc.com/msg_id">sit:GDS:1644644:1442512894123:SRD0IFR119094084</messageId><sentBy messageAddressScheme="http://www.fpml.org/coding-scheme/external/cftc/interim-compliant-identifier">RRasdfjasdfasdkllkd4</sentBy><sendTo>DTCCEU</sendTo><creationTimestamp>2015-10-14T05:54:38Z</creationTimestamp></header></nonpublicExecutionReport> </originalMessage></nonpublicExecutionReportAcknowledgement>
Output required was :- "sit:GDS:1644644:1442512894123:SRD0IFR119094084"
the value should get extracted between
<messageId messageIdScheme="www.abc.com/msg_id"> and </messageId>
Could you please help me to build the regex string for the above problem it will be a great help.
Cheers,
KS
How about:
<inReplyTo messageIdScheme="www.abc.com/msg_id">([a-zA-Z0-9:]+)</inReplyTo>
I am trying to parse the Soap response (snippet shown below) using NSXMLParser. However when I print the Element names in the didStartElemnt delegate method I only get the following elements returned.
Element's name is soap:Envelope
Element's name is soap:Body
Element's name is SearchResponse
Element's name is SearchResult
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchResponse xmlns="http://www.example.com/">
<SearchResult><?xml version="1.0" encoding="utf-8"?><Results xmlns="http://www.example.com/XMLSchema/SearchResult" xmlns:gms="http://www.def.ghi.uk/CM/gms" xmlns:n2=" (more here + further elements....)
Why do I not see the Results (or any subsequent) element?
Most likely it's the second <?xml . . .> directive that appears inside the <SearchResult>. That directive is only allowed at the start of an xml file.
I want to remove XML declaration only from an XML using C++
<?xml version="1.0" encoding="UTF-8" ?>
Then I want to add this line and resave the XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
All I have and know how to do it load the xml document
hr = IXMLDOMDocument->load(vstrfilename, &status);
using the IXMLDOMDocument2 interface of msxml2
How can I achieve this ?
My programming environment is borland c++ builder 6
Thank You
<? some text ?> is a processing instruction. The node is of type NODE_PROCESSING_INSTRUCTION.
Retrieve the node as the first child of the document, using get_childNodes and delete it with removeChild.
Then, use createProcessingInstruction for the new encoding and use insertBefore (with the new first child) to add it to the document.