Solved

VB & XML

Posted on 2002-05-24
5
212 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

930 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

14 Experts available now in Live!

Get 1:1 Help Now