DOMDocument - createTextNode, encoding issue - vb6

I have a problem with createTextNode method e special characters like '>', '<':
Dim xmlDoc As DOMDocument
Dim codeXML as String
Dim n As IXMLDOMNode
codeXML = '<data>value</data>'
Set n = xmlDoc.createTextNode(codeXML)
I need a result like this:
<main><data>value</data></main>
but I get
<main><data>value</data></main>
How can I solve that problem?
Thank you very much.

One way would be to create a second Document to serve as the XML parser for these XML literals. You can use LoadXML to pass the string in and get validated XML objects. You can then Import the node to the original/main DOM Document you are building.

Related

Filter records from xml by date

I want get records from 2014-07-15T00:00:00-05:00 date from below xml. how can i achive this using vbscript? currently i am using this but is not working.
Set colNodes=xmlDoc.selectNodes _
'("//xyz/abc[date>'2014-07-14T00:00:00-05:00']")
<xyz>
<abc>
<date>2014-07-14T00:00:00-05:00</date>
<text>test1</text>
</abc>
<abc>
<date>2014-07-14T00:00:00-05:00</date>
<text>test2</text>
</abc>
<abc>
<date>2014-07-15T00:00:00-05:00</date>
<text>test3</text>
</abc>
<abc>
<date>2014-07-15T00:00:00-05:00</date>
<text>test4</text>
</abc>
<abc>
<date>2014-07-15T00:00:00-05:00</date>
<text>test5</text>
</abc>
</xyz>
Taking a lead from an answer to a related question, I tried this and it seemed to work:
Option Explicit
dim xml: xml = "<xyz><abc><date>2014-07-14T00:00:00-05:00</date><text>test1</text></abc><abc><date>2014-07-14T00:00:00-05:00</date><text>test2</text></abc><abc><date>2014-07-15T00:00:00-05:00</date><text>test3</text></abc><abc><date>2014-07-15T00:00:00-05:00</date><text>test4</text></abc><abc><date>2014-07-15T00:00:00-05:00</date><text>test5</text></abc></xyz>"
'note version 6.0 of the DOM Document
dim xmldoc: set xmldoc = CreateObject("MSXML2.DomDocument.6.0")
xmldoc.async = false
' error check your XML, quit
if not xmldoc.loadXML(xml) then
WScript.Echo xmldoc.parseError.reason
WScript.Quit 1
end if
dim nodes: set nodes = xmldoc.selectNodes("//xyz/abc[number(translate(date, '-.:T', ''))>number(translate('2014-07-14T00:00:00-05:00', '-.:T', ''))]")
' put results into fragment because we won't have a full valid XML document
dim frag: set frag = xmldoc.createDocumentFragment()
dim node
for each node in nodes
frag.appendChild node
next 'node
' use result
WScript.Echo frag.xml
The trick is realising that XPath and XSL 1.0 do not seem to support DateTime types, so to run a numeric comparison we strip any punctuation from the data values using translate() before converting to a number.
One issue with this is it does not take into account your timezone component so you may get incorrect results if dates differ purely by timezone, for example. This is a starting point at least.

Compare two xml files using VBS in QTP

I need to compare 2 xml files using QTP where the values for each tag needs to be compared and need to print the difference in values if found. I used the built in Function XMLUTIL but its not working as expected i.e.. its creates a file with differences including the parent tag.
<tns:AAL_Request_NEW xsi:schemaLocation="http://www.bnymellon.com/AAL_Request_NEW AAL_Request_NEW.xsd">
<tns:OPF_Information>
<tns:Source>
<tns:Source>EPH</tns:Source>
</tns:Source>
<tns:References>
<tns:Accounting_Record_Reference>130830000672401</tns:Accounting_Record_Reference>
<tns:OPF_Reference>EPH1308300006724</tns:OPF_Reference>
<tns:Group_Reference>EPH1308300006723</tns:Group_Reference>
</tns:References>
</tns:OPF_Information>
</tns:AAL_Request_NEW>
In the above xml file i just need the tags with values like
tns:Source with value EPH, tns:Accounting_Record_Reference with value 130830000672401, tns:OPF_Reference with value EPH1308300006724 and tns:Group_Reference EPH1308300006723 to be compared and not the parent tags like tns:References, tns:OPF_Information or tns:AAL_Request_NEW.
Can anyone help with the logic to fetch the tags which has no child tag inside it and ends immediately with having only a value between its start <> and end and compare it with the other file and print the tag name and the values if there is a difference?
you can use CreateObject("Microsoft.XMLDOM") to read the xml files and retrive the tags by tag name and comparte them both.
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("<XML PATH>")
Set Root = objXMLDoc.documentElement
Set tags = root.tagnames
Set NodeList = Root.getElementsByTagname("<node name>")
For Each Elem In NodeList
msgbox Elem.text
Next
Thanks and regards

Trouble with using multiple actions within a if statement

everytime i code using multiple actions, it doesnt produce and output and the code doesnt work at all until i give it one action alone instead of three, i dont know whats wrong with it, i tried to put this code in if statements and/or just left it alone as an action when pressing the save button
heres the code \ btw im using visual studio 2012
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim Path1 As String = "Backups\"
Dim Path2 As String = rtbTitle.Text + "\"
Dim FullPath As String = Path1 + Path2
Dim textfileTitle As String = "title.txt"
Dim textfileDescription As String = "description.txt"
Dim textfileTag As String = "tag.txt"
Dim textfileChannel As String = "channel.txt"
If Directory.Exists(FullPath) Then
rtbTitle.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
rtbDescription.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
rtbTag.SaveFile(FullPath + textfileDescription, RichTextBoxStreamType.PlainText)
End If
End Sub
Add Option Explicit and Option Strict to the top of your code file (or set them as defaults) and you'll soon see where you're getting into trouble.
One problem that will show up is that you're using '+' to concatenate strings, you really should use '&'. You may unwittingly have typos in your control names or be inputting bad data for the paths. All these sorts of errors are covered up until run time unless you set those options.
After that add a Try...Catch block around your code, run it and view the error.

Getting Type Mismatch Issue while Adding parameter to XSLT using VBScript

I have got below code in VBScript:
<%
Option Explicit
#importXSLT "tcm:228-190529-2048" As expandXSLT
#importXSLT "tcm:228-642694-2048" As renderXSLT
Dim xml, currentDateTime
Set xml = getNewDomDocument()
xml.loadXML TDSE.GetListPublications(3)
expandXSLT.input = xml
Call expandXSLT.addParameter("publication", Component.Publication.Id)
expandXSLT.transform
xml.loadXML(expandXSLT.output)
'WriteOut xml.xml
currentDateTime = now
renderXSLT.input = xml
Call renderXSLT.addParameter("currentPublishedDate", currentDateTime)
renderXSLT.transform
WriteOut renderXSLT.output
Set xml = Nothing
%>
You can see there is two syntax where I am adding the XSLT parameter, the first one is working fine i.e.
expandXSLT.input = xml
Call expandXSLT.addParameter("publication", Component.Publication.Id)
expandXSLT.transform
However the new requirement was that, we need to send current date time from here to the XSLT, so I have added the same logic to send the current date & time to my XSLT, below is code added by me
currentDateTime = now
renderXSLT.input = xml
Call renderXSLT.addParameter("currentPublishedDate", currentDateTime)
renderXSLT.transform
But when I am trying to run the above code its giving below error:
Type mismatch.
(source: Call renderXSLT.addParameter("publishedDate", currentDateTime)).
Please suggest!!
Try whether converting the currentDateTime value you have to a string first to pass that string to XSLT e.g. CStr(currentDateTime).

MSXML2.DOMDocument.xml give me malformed xml

We have an old legacy system that where a component is writter in VB6. One method returns a string that is xml data. The xml data is created with msxml3.dll MSXML2.DOMDocument and returns the data of the document with the property xml: http://msdn.microsoft.com/en-us/library/ms755989(v=VS.85).aspx
However, some data of the xmldocument is from the database and one field is a hashed password string. The code that set the data for the element:
Set cellNode = rowNode.appendChild(xml.createElement("COL"))
If IsNull(rs(oField.name).Value) Then
cellNode.Text = ""
Else
cellNode.Text = rs(oField.name).Value
End If
This gives me malformed/non-wellformed xml:
<ROWS><ROW><COL>r<í</COL></ROW></ROWS>
Is there a workaround for this?
You should escape unicode characters. Or put them in a CDATA tag (which is not such a nice solution though)
Btw < > and & should be escaped as well.

Resources