Restoring "InnerXML" to a node.
I have a need (which I won't bother to elaborate on) to get the "InnerXML"
of a node from a string value, which I can do by:
Private Function Text2XML(ByVal Tx As String)
Dim xDoc As MSXML2.DOMDocument
Dim xNode As MSXML2.IXMLDOMNode
Set xDoc = New MSXML2.DOMDocument
Set xNode = xDoc.appendChild(xDoc.createElement("DocNode"))
xNode.Text = txtSource.Text
Text2XML = xNode.childNodes(0).xml
Set xNode = Nothing
Set xDoc = Nothing
End Function
It will turn "Johnson & Johnson" into "Johnson & Johnson"
However, I would like to be able to "validate" the results from the original
string, so I have a "counterpart" function:
Private Function XML2Text(ByVal stXML As String) As String
Dim xDoc As MSXML2.DOMDocument
stXML = "<BaseTag>" & stXML & "</BaseTag>"
Set xDoc = New MSXML2.DOMDocument
xDoc.loadXML (stXML)
XML2Text = xDoc.Text
Set xDoc = Nothing
End Function
Which does return the string back to me intact and identical to the original
that was supplied to the first function.
However, it seems to me that concatenating tags around the submitted stXML
seems kind of "klunky," and I am wondering if there is a better way to load
the "Johnson & Johnson" back into the XML parser (Microsoft's XML3) and
accomplish the same thing without concatenating the "<BaseTag>" and
"</BaseTag>" around the string manually before it goes into the XML parser?
Any thoughts?
# 1 Re: Restoring "InnerXML" to a node.
Many object oriented solutions could be more elegant and cleaner than
concatenation, but the concatentation, requires less code on your part. By
keeping your solution the way it is, you'll save on visual complexity,
object creation and maintenance.
"Wesley Long" <weslong@wesNOSPAMware.com> wrote in message
news:3dda6bba@tnews.web.dev-archive.com...
> I have a need (which I won't bother to elaborate on) to get the "InnerXML"
> of a node from a string value, which I can do by:
>
> Private Function Text2XML(ByVal Tx As String)
> Dim xDoc As MSXML2.DOMDocument
> Dim xNode As MSXML2.IXMLDOMNode
>
> Set xDoc = New MSXML2.DOMDocument
> Set xNode = xDoc.appendChild(xDoc.createElement("DocNode"))
> xNode.Text = txtSource.Text
>
> Text2XML = xNode.childNodes(0).xml
>
> Set xNode = Nothing
> Set xDoc = Nothing
>
> End Function
>
> It will turn "Johnson & Johnson" into "Johnson & Johnson"
>
>
> However, I would like to be able to "validate" the results from the
original
> string, so I have a "counterpart" function:
>
> Private Function XML2Text(ByVal stXML As String) As String
> Dim xDoc As MSXML2.DOMDocument
>
> stXML = "<BaseTag>" & stXML & "</BaseTag>"
>
> Set xDoc = New MSXML2.DOMDocument
> xDoc.loadXML (stXML)
> XML2Text = xDoc.Text
>
> Set xDoc = Nothing
>
> End Function
>
> Which does return the string back to me intact and identical to the
original
> that was supplied to the first function.
>
> However, it seems to me that concatenating tags around the submitted stXML
> seems kind of "klunky," and I am wondering if there is a better way to
load
> the "Johnson & Johnson" back into the XML parser (Microsoft's XML3)
and
> accomplish the same thing without concatenating the "<BaseTag>" and
> "</BaseTag>" around the string manually before it goes into the XML
parser?
>
> Any thoughts?
>
>
# 2 Re: Restoring "InnerXML" to a node.
Agreed.
However, the "validation" function (XML2Text) is only for development
testing, and will not be part of the final application, so I was hoping to
use the opportunity to become more familiar with some of the subtleties of
the XML parser.
It has generally been my experience that things that are "taken apart" are
usually best "reassembled" with the same methods and tools. I was wondering
if there was a way to turn the XML string back into a node object strictly
within the MSXML parser. However, since the XML property of a node is
read-only, and only the DOMDocument object seems to contain a "loadXML"
method (which requires the string to be a complete, well-formed XML
document), perhaps there is not an equivalent available after all.
"Michael Gautier" <gautier_michael@hotmail.com> wrote in message
news:3ddad383$1@tnews.web.dev-archive.com...
> Many object oriented solutions could be more elegant and cleaner than
> concatenation, but the concatentation, requires less code on your part. By
> keeping your solution the way it is, you'll save on visual complexity,
> object creation and maintenance.
>
>
>
> "Wesley Long" <weslong@wesNOSPAMware.com> wrote in message
> news:3dda6bba@tnews.web.dev-archive.com...
> > I have a need (which I won't bother to elaborate on) to get the
"InnerXML"
> > of a node from a string value, which I can do by:
> >
> > Private Function Text2XML(ByVal Tx As String)
> > Dim xDoc As MSXML2.DOMDocument
> > Dim xNode As MSXML2.IXMLDOMNode
> >
> > Set xDoc = New MSXML2.DOMDocument
> > Set xNode = xDoc.appendChild(xDoc.createElement("DocNode"))
> > xNode.Text = txtSource.Text
> >
> > Text2XML = xNode.childNodes(0).xml
> >
> > Set xNode = Nothing
> > Set xDoc = Nothing
> >
> > End Function
> >
> > It will turn "Johnson & Johnson" into "Johnson & Johnson"
> >
> >
> > However, I would like to be able to "validate" the results from the
> original
> > string, so I have a "counterpart" function:
> >
> > Private Function XML2Text(ByVal stXML As String) As String
> > Dim xDoc As MSXML2.DOMDocument
> >
> > stXML = "<BaseTag>" & stXML & "</BaseTag>"
> >
> > Set xDoc = New MSXML2.DOMDocument
> > xDoc.loadXML (stXML)
> > XML2Text = xDoc.Text
> >
> > Set xDoc = Nothing
> >
> > End Function
> >
> > Which does return the string back to me intact and identical to the
> original
> > that was supplied to the first function.
> >
> > However, it seems to me that concatenating tags around the submitted
stXML
> > seems kind of "klunky," and I am wondering if there is a better way to
> load
> > the "Johnson & Johnson" back into the XML parser (Microsoft's XML3)
> and
> > accomplish the same thing without concatenating the "<BaseTag>" and
> > "</BaseTag>" around the string manually before it goes into the XML
> parser?
> >
> > Any thoughts?
> >
> >
>
>
