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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create XML File from SCRATCH via VB or Java ..?

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Many people tend to use DOM to read XML files in VB and other languages.

I am currently accessing DOM through VB with this code structure:
Code:
  Dim xmldoc As DOMDocument
  Dim x As IXMLDOMNode
  
  Set xmldoc = New DOMDocument
  xmldoc.async = "false"
  xmldoc.Load (App.Path & "\xml_test.xml")
  Text1 = "Traversing the XML nodes"
  For Each x In xmldoc.documentElement.childNodes
    Text1 = Text1 & x.nodeName
    Text1 = Text1 & ": "
    Text1 = Text1 & x.Text
    Text1 = Text1 & vbCrLf & vbCrLf
    If x.nodeName = "" Then x.Set
  Next
  xmldoc.save (App.Path & "\xml_test2.xml")


But is it possible to build an XML file from scratch from within VB or Java (compiled or scripted) then export the resulting 'Virtual XML Document' to an XML file using a command such as:
xmldoc.save (App.Path & "\xml_test.xml")

If so, could someone give a small example of what commands to use (prefer VB code, but Java will work too)

Here is a Sample Target Structure to build for the Example...
Code:
<TEST>
  <OBJ Type="Type1">
    <NAME>Object 1</NAME>
    <ITEM>Test Item1</ITEM>
    <ITEM>Test Item2</ITEM>
  </OBJ>
  <OBJ Type="Type2">
    <NAME>Object 2</NAME>
    <ITEM>Test Item</ITEM>
  </OBJ>
</TEST>

If you don't feel like writing an example, but know of a link where I could learn to do this, Please Post It :)

Also, if you wanted to Change the Type in the second OBJ from "Type2" to "Type3" how could you do that?

And/Or... Change:
Code:
<NAME>[b][COLOR=red]Object 2[/color][/b]</NAME>
To:
Code:
<NAME>[b][COLOR=red]Object 3[/color][/b]</NAME>

Much Thanks in Advance,
-Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Hmmm... No answer...

OK, To answer my own question... ;-)

YES you can, like this:

Code:
  Dim xmldoc As DOMDocument
  Dim x As IXMLDOMElement
  Dim a As IXMLDOMAttribute
  
  Set xmldoc = New DOMDocument
  xmldoc.appendChild xmldoc.createElement("TEST")
  
  Set x = xmldoc.documentElement.appendChild(xmldoc.createElement("OBJ"))
  Set a = xmldoc.createAttribute("Type")
  a.Text = "Type1"
  x.setAttributeNode a
  x.appendChild(xmldoc.createElement("NAME")).Text = "Object 1"
  x.appendChild(xmldoc.createElement("ITEM")).Text = "Test Item1"
  x.appendChild(xmldoc.createElement("ITEM")).Text = "Test Item2"
  
  Set x = xmldoc.documentElement.appendChild(xmldoc.createElement("OBJ"))
  Set a = xmldoc.createAttribute("Type")
  a.Text = "Type2"
  x.setAttributeNode a
  x.appendChild(xmldoc.createElement("NAME")).Text = "Object 2"
  x.appendChild(xmldoc.createElement("ITEM")).Text = "Test Item"
  
  xmldoc.save (App.Path & "\MyTest1.xml")

I figured I would Post this anyways, just in case anyone else was curious ;-)

Here Are 2 very good sources:

Hope this helps someone else in the future :)

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
And to answer the second part of my question (changing values) you just traverse through it and change the text property...

Code:
  Dim xmldoc As DOMDocument
  Dim x As IXMLDOMElement
  Dim y As IXMLDOMElement
  Dim a As IXMLDOMAttribute
  
  Set xmldoc = New DOMDocument
  xmldoc.async = "false"
  xmldoc.Load (App.Path & "\MyTest1.xml")
  For Each x In xmldoc.documentElement.childNodes
    Set a = x.getAttributeNode("Type")
    If a.Text = "Type2" Then a.Text = "Type3"
    If x.hasChildNodes Then
      For Each y In x.childNodes
        If y.nodeName = "NAME" And y.Text = "Object 2" Then y.Text = "Object 3"
      Next
    End If
  Next
  xmldoc.save (App.Path & "\MyTest2.xml")

I'm sure there is a better way to do this, so you still have a chance to post a better answer ;-)

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top