I am trying to write an XML file using MSXML4. It works fine except when I have a data element with a trailing space which must be preserved.
Given the following code to insert a new element:
const _bstr_t k_Parent (ToBSTR("ParentNode"));
const _bstr_t k_Child (ToBSTR("ChildNode"));
const _bstr_t k_Data (ToBSTR("DataWithTrailingSpace "));
const _bstr_t k_Namespace (ToBSTR("TheNameSpace"));
MSXML2::IXMLDOMDocument2Ptr m_pXmlDoc;
m_pXmlDoc->async = VARIANT_FALSE;
m_pXmlDoc->validateOnParse = VARIANT_FALSE;
m_pXmlDoc->resolveExternals = VARIANT_FALSE;
m_pXmlDoc->preserveWhiteSpace = VARIANT_TRUE;
MSXML2::IXMLDOMNodePtr pElement = m_pXmlDoc->createNode(NODE_ELEMENT, k_Child, k_Namespace);
MSXML2::IXMLDOMNodePtr pParent = m_pXmlDoc->selectSingleNode(k_Parent);
pElement->put_text (k_Data);
MSXML2::IXMLDOMNodePtr pNewChild = pParent->appendChild(pElement);
If I check "pNewChild->text", the text still contains the trailing space. When I try writing it to a file:
std::string xml (static_cast<std::string>(m_pXmlDoc->xml));
std::ofstream file("output.xml");
file << xml << std::endl;
file.flush();
file.close();
The output is:
<ParentNode>
<ChildNode>DataWithTrailingSpace</ChildNode>
</ParentNode>
Instead of (note the extra space behind "DataWithTrailingSpace"):
<ParentNode>
<ChildNode>DataWithTrailingSpace </ChildNode>
</ParentNode>
I cannot figure out at what point the trailing space is getting stripped.
Can someone please provide some insights in to where this may be occurring and how I can correct it?
If you need to preserve whitespace then you should be using a CDATA section via createCDATASection() or the like.
Mystery solved. Don't preview your XML in Internet Explorer. It hides trailing spaces. Use notepad instead.
You should replace the whitespace(s) with . That way your whitespaces should persist.
EDIT
Appearantly it didn't solve your problem. Then maybe you want to have a look at these sites:
http://msdn.microsoft.com/en-us/library/ms757008(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms757885(VS.85).aspx
Related
I'm trying to solve an issue with posting comments for a blog that uses the Weblog Sitecore module. From what I can tell, if the blog entry url contains dashes (i.e. http://[domain.org]/blog/2016/december/test-2-entry), then I get the "End of string expected at line [#]" error. If the blog entry url does NOT contain dashes, then the comment form works fine.
<replace mode="on" find="-" replaceWith="_"/>
Also tried to replace the dash with an empty space. Neither solution has worked as I still get the error.
Is there some other setting in the Web.config I can alter to escape the dashes in the urls? I have read that enclosing dashed url text with the # symbol works, but I'd like to be able to do that automatically instead of having the user go back and rename all their blog entries.
Here is a screenshot of the error for reference:
I have not experience the Weblog module but for the issue you are facing, you should escape the dash with #. Please see the following code snippet:
public string EscapePath(string path)
{
string[] joints = Regex.Split(path, "/");
string output = string.Empty;
for (int index = 0; index < joints.Length; index++)
{
string joint = joints[index];
if (!string.IsNullOrEmpty(joint))
output += string.Format("#{0}#", joint);
if (index != joints.Length - 1)
output += "/";
}
return output;
}
Reference: https://github.com/WeTeam/WeBlog/issues/52
More information about escaping dash in queries can be found here
UPDATE
You should call this method before posting the comment for it to escape the dashes. You may also download the dll from here and use it in your solution
I am using docx4j 2.8.1 with Content Controls in my .docx file. I can replace the CustomXML part by injecting my own XML and then calling BindingHandler.applyBindings after supplying the input XML. I can add a token in my XML such as ¶ then I would like to replace that token in the MainDocumentPart, but using that approach, when I iterate through the content in the MainDocumentPart with this (link) method none of my text from my XML is even in the collection extracted from the MainDocumentPart. I am thinking that even after binding the XML, it remains separate from the MainDocumentPart (??)
I haven't tried this with anything more than a little test doc yet. My token is the Pilcrow: ¶. Since it's a single character, it won't be split in separate runs. My code is:
private void injectXml (WordprocessingMLPackage wordMLPackage) throws JAXBException {
MainDocumentPart part = wordMLPackage.getMainDocumentPart();
String xml = XmlUtils.marshaltoString(part.getJaxbElement(), true);
xml = xml.replaceAll("¶", "</w:t><w:br/><w:t>");
Object obj = XmlUtils.unmarshalString(xml);
part.setJaxbElement((Document) obj);
}
The pilcrow character comes from the XML and is injected by applying the XML bindings to the content controls. The problem is that the content from the XML does not seem to be in the MainDocumentPart so the replace doesn't work.
(Using docx4j 2.8.1)
I have string with xml content, writing it into a file and reading it back with xmlParseFile affects performance, if there is a way to parse the string directly, can you please show it with an example?
Consider xmlParseMemory instead.
I have string with xml content
So you should be able to do it like so:
const std::string xmlContent = "<something> </something>";
xmlDocPtr doc = xmlParseMemory(xmlContent.c_str(), xmlContent.length());
I have some problems with editing existing child node, I don't know if its possible but I hope it is. I tried almost everything.
This is part of code that I use to retrieve first childnode text
int x;
void ReadX()
{
String ^ strFilename = L"test.xml";
XmlDocument ^ testxml = gcnew XmlDocument;
if (File::Exists(strFilename))
{
testxml->Load(strFilename);
XmlElement ^ elm = testxml->DocumentElement;
XmlNodeList ^ lstVideos = elm->ChildNodes;
//MessageBox::Show(elm->Name);
//MessageBox::Show(elm->InnerText);
x = Convert::ToInt32(lstVideos[0]->InnerText);
}
and on button click
textBox1->Text=System::Convert::ToString(x);
What i would like to do is on button2 click to input value from textBox1 to lstVideos[0]->InnerText
Please don't link me msdn because I already worked with their examples, and I didn't manage to solve it.
I still have to learn a lot about xml, what it would to is edit text of X1, my code above reads X1's text. Would probably be easier to read childnode by name, but I don't yet know how to do that.
This is my xml file
<?xml version="1.0" encoding="utf-8"?>
<Coords>
<X1>10010</X1>
<X2>200</X2>
<X3>300</X3>
<X4>400</X4>
<X5>500</X5>
</Coords>
Since InnerText is a String^ and textBox1->Text is a String^, why not:
lstVideos[0]->InnerText = textBox1->Text
Also, since both are strings, there is really no reason to convert the InnerText to an int, then back in the first place.
I'm trying to append text to a rich edit control by appending the original string and resending the EM_SETTEXTEX message.
char outputText[4096] = "{\\rtf1\\ansi\\ansicpg0\\deff0{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red50\\green205\\blue50;\\red255\\green140\\blue0;}TEST";
SETTEXTEX s;s.flags = ST_DEFAULT;s.codepage = CP_ACP;
SendMessage(hOutputWndText,EM_SETTEXTMODE,(WPARAM)TM_RICHTEXT,NULL);
SendMessage(hOutputWndText,EM_SETTEXTEX,(WPARAM)&s,(LPARAM)outputText);
I know I do not have a closing bracket on the string but it shows what I want.
TEST
Now I append the string and "re-set" the text inside the rich edit control. Notice, I add a closing bracket just incase.
strcat_s(outputText,"NEWSTUFF}");
SendMessage(hOutputWndText,EM_SETTEXTEX,(WPARAM)&s,(LPARAM)outputText);
And the output this time.
NEWSTUFF}
What gives? I printed the variable outputText to the console and I get the complete string.
{\rtf1\ansi\ansicpg0\deff0{\colortbl;\red0\green0\blue0;\red255\green0\blue0;\red50\green205\blue50;\red255\green140\blue0;}TESTNEWSTUFF}