Solved

XML duplicate empty namespace in child nodes. Why ?

Posted on 2007-12-05
2
2,114 Views
Last Modified: 2013-11-26
I want to create in VB6 a KML file for import in Google Earth.

Following the template for KML file (http://code.google.com/apis/kml/documentation/kml_tut.html) I must specify a namespace in root element.

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
  <Placemark>
    <name>Simple placemark</name>
    <description>Attached to the ground. Intelligently places itself
       at the height of the underlying terrain.</description>
    <Point>
      <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
    </Point>
  </Placemark>
</kml>

But after execute my program (see code) I have this result:

<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type='text/xml' href='test.xsl'?>
      <!-- sample xml file created using XML DOM object.-->
<root xmlns="http://earth.google.com/kml/2.0" created="2007.12.05" language="vb6">
      <node1 xmlns="">some character data</node1>
            <node2 xmlns="">
                  <![CDATA[ <some mark-up text>]]>
            </node2>
      <node3 xmlns="">
            <subNode1 />
            <subNode2 test="wow">other character data</subNode2>
            <subNode3 />
      </node3>
</root>

As you can see, xmlns attribute (xmlns="") is repeated in every child node but I don't want.
Any idea so tolve the problem ?


Set DOM = New DOMDocument60

  

  With DOM

    .async = False

    .preserveWhiteSpace = False

    .resolveExternals = False

    .validateOnParse = False

  End With

  

  ' Create a processing instruction targeted for xml.

  Set ProcessingInstruction = DOM.createProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8""")

  DOM.appendChild ProcessingInstruction

  Set ProcessingInstruction = Nothing

  

  ' Create a processing instruction targeted for xml-stylesheet.

  Set ProcessingInstruction = DOM.createProcessingInstruction("xml-stylesheet", "type='text/xml' href='test.xsl'")

  DOM.appendChild ProcessingInstruction

  Set NodeElement = Nothing

  

  ' Create a comment for the document.

  Set Comment = DOM.createComment("sample xml file created using XML DOM object.")

  DOM.appendChild Comment

  Set Comment = Nothing

  

  ' Create the root element

  Set RootElement = DOM.createElement("root")

  

  Set Attr = DOM.createAttribute("xmlns")

  Attr.Value = "http://earth.google.com/kml/2.0"

  Set Attr = Nothing

  

  ' Create a "created" attribute for the root element and assign the "2007.12.05" character data as the attribute value.

  Set Attr = DOM.createAttribute("created")

  Attr.Value = "2007.12.05"

  RootElement.setAttributeNode Attr

  Set Attr = Nothing

  

  ' Create a "language" attribute for the root element and assign the "vb6" character data as the attribute value.

  Set Attr = DOM.createAttribute("language")

  Attr.Value = "vb6"

  RootElement.setAttributeNode Attr

  Set Attr = Nothing

  

  ' Add the root element to the DOM instance.

  DOM.appendChild RootElement

  

  ' Create and add one more node to the root element just created.

  

  ' Create a text element.

  Set NodeElement = DOM.createElement("node1")

  NodeElement.Text = "some character data"

  

  ' Add text node to the root element.

  RootElement.appendChild NodeElement

  Set NodeElement = Nothing

	

	and so on...

Open in new window

0
Comment
Question by:peter1950
2 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 20413044
Create your root element with a default namespace like so:

 Dim DefaultNS As String
 DefaultNS = "http://earth.google.com/kml/2.0"
 ' Create the root element
 Set RootElement = Dom.createNode(NODE_ELEMENT, "root", DefaultNS)

Now, add your child nodes with the namespace as well:

 ' Create a text element.
 Set nodeElement = Dom.createNode(NODE_ELEMENT, "node1", DefaultNS)
 nodeElement.Text = "some character data"
0
 

Author Closing Comment

by:peter1950
ID: 31412878
Thanks
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

Title # Comments Views Activity
SlingPlayer alternatives 1 28
Excel file not created as expected 7 46
Problem with Filesystemobject or .NET version 3 31
Run time Error 4 34
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

939 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

5 Experts available now in Live!

Get 1:1 Help Now