Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML file generation using DOM parser

Status
Not open for further replies.

malis

Programmer
Mar 3, 2002
2
IL
Is there a way to construct the DOM tree from memory and not from an XML file?
Is there a way to convert DOM tree into XML and prints it into file and not to the screen?
 
hi.
im not sure that this is what you need but anyway here some example code in vb that shows loading un saving a file.xml
(this code also comes with some gui components to view the data):

Option Explicit
Private xmlDoc As DOMDocument
Private style As IXMLDOMNode
Private nodesList As IXMLDOMNodeList
Private m_AppPath As String

' Add a new node to the indicated parent node.
Private Function createNode(ByVal indent As Integer, ByVal parent As IXMLDOMNode, ByVal node_name As String, ByVal style As String) As IXMLDOMNode
Dim newNode As IXMLDOMNode
parent.appendChild parent.ownerDocument.createTextNode(vbCrLf)
' Indent.
parent.appendChild parent.ownerDocument.createTextNode(Space$(indent))

' Create the new node.
Set newNode = parent.ownerDocument.createElement(node_name)


' Set the node's text value.
newNode.Text = style
Set createNode = newNode
End Function
Private Sub appNode(ByVal parent As IXMLDOMNode, ByVal newNode As IXMLDOMNode)
'appends node to parent
parent.appendChild newNode
parent.appendChild parent.ownerDocument.createTextNode(vbCrLf)
End Sub

' Load saved values from XML file.
Public Sub loadValues()
' Load the document.
xmlDoc.Load m_AppPath & "Values.xml"

If xmlDoc.documentElement Is Nothing Then
MsgBox (m_AppPath & "Values.xml")
Exit Sub
End If
'set the parent node
Set style = xmlDoc.selectSingleNode("Style")
'insert all child nodes into object nodesList
Set nodesList = xmlDoc.selectNodes("Style/Atr")
makeList
End Sub
Private Sub makeList()
Dim i As Integer
For i = 0 To nodesList.length - 1
List1.AddItem nodesList.Item(i).Text
Next
End Sub
Public Sub addValues()
Dim newNode As IXMLDOMNode
'create node
Set newNode = createNode(4, style, "Atr", att.Text)
'append node
appNode style, newNode
' Save the XML document.

List1.AddItem (att.Text)
List1.Refresh
Set newNode = Nothing
End Sub

Public Sub saveValues()
xmlDoc.save m_AppPath & "Values.xml"
End Sub
Public Sub updateNode()
nodesList.Item(List1.ListIndex).Text = att.Text
List1.AddItem att.Text, List1.ListIndex
List1.RemoveItem List1.ListIndex
List1.Refresh
End Sub
Public Sub removeNode()
Dim node As IXMLDOMNode
Set node = nodesList.Item(List1.ListIndex)
style.removeChild node
Set node = Nothing
List1.RemoveItem (List1.ListIndex)
List1.Refresh
End Sub
Private Function isSelected() As Boolean
Dim flag As Boolean
flag = True
If List1.ListIndex = -1 Then
MsgBox ("You have to select an item first")
flag = False
End If
isSelected = flag
End Function
Private Function isFilled() As Boolean
Dim flag As Boolean
flag = True
If att.Text = "" Then
MsgBox ("You have to insert a value first")
flag = False
End If
isFilled = flag
End Function

Private Sub Command1_Click()
saveValues
End Sub

Private Sub Command2_Click()
If Not isSelected Or Not isFilled Then Exit Sub
updateNode

End Sub

Private Sub Command3_Click()
If Not isSelected Then Exit Sub
removeNode
End Sub

Private Sub Command4_Click()
If Not isFilled Then Exit Sub
addValues
End Sub

Private Sub Form_Load()
' Get the application's startup path.
Set xmlDoc = New DOMDocument
m_AppPath = App.Path
If Right$(m_AppPath, 1) <> &quot;\&quot; Then m_AppPath = m_AppPath & &quot;\&quot;

' Load the values.
loadValues
End Sub


any further Q are welcome!!!

raikyng
 
Hi Raikyng,

Thanks a lot for your quick respond.
Is there a way to write this code in c/c++?

Thanks again,

mali
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top