Solved

VB & XML

Posted on 2002-05-24
5
211 Views
Last Modified: 2010-05-02
Hi,
I am trying to add an xml string in the format "<problem><needHelp>problemId</needHelp></problem>"as well as adding the XML stored in another DOM into One single DOM to process. What I am doing at present is calling the below sub and passing in the element to add onto , the new name and the String or the DOM.xml respectively.  

However, the problem I am having is that when I add this onto the DOM the dom treats it as a text node and converts the < into &lt; and > into &gt;

In java I would use importNode is there a MSXML2 equivalent or is there a workaround.

Public Sub addValueElement(ByRef parent As MSXML2.IXMLDOMNode, name As String, value As Variant)

Dim doc As MSXML2.DOMDocument
Dim child As MSXML2.IXMLDOMElement
'get the owner Document of the Node
Set doc = parent.ownerDocument
'create the new Element
Set child = doc.createElement(name)
If value <> "" Then
    'give the element a child text node
    'containing the value
    child.appendChild doc.createTextNode(value)
End If
'append the child node onto the element
parent.appendChild child
End Sub
0
Comment
Question by:adamgernon
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7032357
Load the string "<problem><needHelp>problemId</needHelp></problem>" to a second XMLDocument (xml2).  Then use the appendChild method to load the documentElement from xml2

If this does not make any sense, I can give you some code.

Also, when you get a chance please maintain your open questions:
Questions Asked 40
Last 10 Grades Given A A A C B A A A B A  
Question Grading Record 27 Answers Graded / 27 Answers Received

Thanks,
Anthony
0
 

Author Comment

by:adamgernon
ID: 7032415
I tried this but It said u can have only one top level so if u wouldn't mind a small bit of code would be great
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 50 total points
ID: 7032671
I see what you mean (that comes from commenting without testing <g>).

Assuming that you have a main XML Document "<root/>" and you want to append "<problem><needHelp>problemId</needHelp></problem>", then the following code is what you need:

Dim xmlMainDoc As MSXML2.DOMDocument
Dim xmlFragDoc As MSXML2.DOMDocument

Set xmlMainDoc = New MSXML2.DOMDocument
If xmlMainDoc.loadXML("<root/>") Then
    Set xmlFragDoc = New MSXML2.DOMDocument
    If xmlFragDoc.loadXML("<problem><needHelp>problemId</needHelp></problem>") Then
        xmlMainDoc.documentElement.appendChild xmlFragDoc.documentElement
    End If
    Set xmlFragDoc = Nothing
    Debug.Print xmlMainDoc.xml
End If
Set xmlMainDoc = Nothing

If you want to add the child nodes somewhere else other than the <root>, than first set that node using the SelectSingleNode method, for example, and then do the appendChild to that.

Again, let me know if this does not make any sense.

Anthony
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 7033405
Also I see you have maintained most of your open questions, but I believe you may have overlooked this one:
Word Programming in VB Date: 08/01/2001 07:03AM PST  
http://www.experts-exchange.com/jsp/qShow.jsp?ta=winprog&qid=20161553

Anthony


0
 

Author Comment

by:adamgernon
ID: 7038983
thanks for ur help (and the gentle reminder)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB6 convert MSHFlexgrid1 cells 7 52
SSRS expression Issue finding a string 10 51
VBA filters 2 39
Spell Check in VB6 13 45
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now