Solved

VB & XML

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

821 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