how do i add attributes to xml using xerces? - c++

i have currently generated some XML using xercer in C++, using the following code:
XMLCh tempAttribute[100];
XMLString::transcode("ad", tempStr, 99);
doc = impl->createDocument(0,tempStr ,0);
root = doc->getDocumentElement();
XMLString::transcode("imageAd", tempStr, 99);
element = doc->createElement(tempStr);
root->appendChild(element);
However i am attempting to get the attributes within the top "ad" element (as below), however i have had little luck in doing so, can someone with experience using xerces please advise.
Thanks in advance!
<ad xsi:noNamespaceSchemaLocation="smaato_ad_v0.9.xsd" modelVersion="0.9">
<imageAd>

maybe you didn't saw the call to setAttribute in my previous answer but you can set any attribute for any element with calls like
root->setAttribute(L"modelVersion", L"0.9");
root->setAttribute(L"xsi:noNamespaceSchemaLocation", L"xsi:noNamespaceSchemaLocation");
Where root is the pointer to your root element.

Related

How to create a xml node using rapidxml

Hi I want to create the following xml file in C++ using rapidxml on Linux.
How to add an element which is of type name.
<wrapit>
<mainNode>
<name>something1</name>
</mainNode>
</wrapit>
what my code generates looks like following which I don't want.
<wrapit>
<mainNode>
<name something1=""/>
</mainNode>
</wrapit>
I could not find much information for this. Few on wordpress but the xml formats are different.
Code snippet
xml_node<>* root = doc.allocate_node(node_element, "mainNode");
doc.append_node(root);
xml_node<>* child = doc.allocate_node(node_element,"name");
child->append_attribute(doc.allocate_attribute("something1"));
root->append_node(child);
ugg....
xml_node<>* child = doc.allocate_node(node_element,"name","something1");
does it.

How to Create a Child with pair tag with QT

I am working on a project where I have to write information in a XML document using QT Library ( QTDomDocument, etc.) for C++.
It might be stupid to ask, but I am looking for a solution on how to create a XML child node with pair tags, for example, I have this :
<color_space>
</color_space>
And I would like to add x childs following this schema:
<color_space>
<color_plan>R</color_plan>
<color_plan>G</color_plan>
...
</color_space>
I have tried multiple different codes and as far I did, the most similar resultat that I had was :
<color_space>
<color_plan/>
<color_plan/>
...
</color_space>
Thank you all for your help.
Ok so I Just Figured out how to do what I attempted to do, I hope it will help other people!
// Here we have a QVector where we have all our color plan
QVector<QString>ColorPlanTable= { "R","G","B","L","a","b","Y","Cb","Cr" };
//sub_element is the Color_space element (parent node)
sub_element= sub_element.nextSiblingElement();
// Here we create a new element for color_plan (child of Color_space)
QDomElement NewColorPlan=document_->createElement("color_plan");
// And then we create a TextNode to add to the element color_plan
QDomText NewColorPlanText = document_->createTextNode(ColorPlanTable.at(i));
// Adding the element color_plan as a child of Color_space
sub_element.appendChild(NewColorPlan);
// And then adding the text of the color_plan!
NewColorPlan.appendChild(NewColorPlanText);

Multiple XML namespaces in xerces

I need to create XML files which contain multiple namespaces.
I create the root element with a default namespace, and add another
namespace ("otherNS") with setAttribute().
The problem is, that when i insert an element (with createElement()) which is prefixed with "otherNS",
xerces adds an empty namespace attribute. when I use createElementNS() and explicitly state the otherNS URI, xerces adds the full URI attribute.
In my understanding of XML namespaces, both is wrong. (Also the examples in
http://www.w3schools.com/Xml/xml_namespaces.asp do not repeat the namespace attributes in each element).
This is an example output:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<company xmlns="http://default.namespace.org/NS" xmlns:otherNS="http://other.namespace.org/ONS">
<otherNS:product xmlns="">Xerces-C</otherNS:product>
<otherNS:category xmlns:otherNS="http://other.namespace.org/ONS" idea="great">XML Parsing Tools</otherNS:category>
<developedBy xmlns="">Apache Software Foundation</developedBy>
</company>
And this is the code:
DOMDocument* doc = impl->createDocument(
X("http://default.namespace.org/NS"),
X("company"),
0);
DOMElement* rootElem = doc->getDocumentElement();
rootElem->setAttribute(
X("xmlns:otherNS"),
X("http://other.namespace.org/ONS"));
DOMElement* prodElem = doc->createElement(X("otherNS:product"));
rootElem->appendChild(prodElem);
DOMText* prodDataVal = doc->createTextNode(X("Xerces-C"));
prodElem->appendChild(prodDataVal);
DOMElement* catElem = doc->createElementNS(
X("http://other.namespace.org/ONS"),
X("otherNS:category"));
rootElem->appendChild(catElem);
My questions are:
Is my usage of the Xerces API correct? Do I maybe have to add the second namespace differently, it seems that xerces does not recognize it.
Are there maybe any features of the DOMLSSerializer class, which change that behaviour, so far I have not found any.
I got the solution on the Xerces mailing list:
Replace:
rootElem->setAttribute(
X("xmlns:otherNS"),
X("http://other.namespace.org/ONS"));
with:
rootElem->setAttributeNS(X("http://www.w3.org/2000/xmlns/"),
X("xmlns:otherNS"),
X("http://other.namespace.org/ONS"));
The reason: the namespace definition itself must be located in the xmlns namespace, so the setAttributeNS() method has to be used.

Reading theme1.xml from a .docx, attribute without namespace

Im making a docx reader (libopc and C++) and I have problem when I want to get the minor and major Font from the theme1.xml. The problem is that I dont know how I have to write the namespace for attributes without it:
<a:latin typeface="Calibri"/>
I have tryed with:
mce_start_attribute(&reader, _X(""), _X("typeface")) {//type
_majorFont = (char*) xmlTextReaderConstValue(reader.reader);
}mce_end_attribute(&reader);
and:
mce_start_attribute(&reader, _X("http://www.3w.org/2000/xmlns"), _X("typeface")) {//type
_majorFont = (char*) xmlTextReaderConstValue(reader.reader);
}mce_end_attribute(&reader);
And I get the same result: nothing.
Any Suggestion.
Thanks in advance.
I answer myself.
After ckeck libopc source code the solution is set namespace value in mce_start_attribute macro as NULL:
mce_start_attribute(&reader, NULL, _X("typeface")) {//type
_majorFont = (char*) xmlTextReaderConstValue(reader.reader);
}mce_end_attribute(&reader);

XML Getting attibute value of a Node

I am using XML DOM API in C++ to parse an XML file. I can't find any method to get the attribute value in a node element.
For example, in the following xml
<test>
<fruit count="10">
...
...
</fruit>
<test>
I need to get the count string("10") using XML APIs. Can anybody help me with some code snippets.
Use DOM Parser API to get attribute value count.
Refer below sample code:
//code to perform some process for parsing the input file and get rootElement
DOMNodeList* fruitNodes= rootElement->getElementsByTagName(XMLString::transcode("fruit"));
DOMNode* node = fruitNodes->item(0);
DOMElement* fruitElement = dynamic_cast <xercesc::DOMElement*>(node);
const XMLCh* attrValue = fruitElement->getAttribute(XMLString::transcode("count"));
you can get value 10 from attrValue using: string(XMLString::transcode(attrValue))
Based on http://msdn.microsoft.com/en-us/library/windows/desktop/ms754523(v=vs.85).aspx
Try something like:
pXMLDomNodeList = pXMLDocElement->selectNodes("/test/fruit/#count");