XML library

Aspire89

Member
Licensed User
please help
How do I add a second line and save the file:

B4X:
<skin name="abc">
<text size="80" color="#ffffff"/>
</skin>

thanks
 
Last edited:

Hitec

Member
Licensed User
Namespace of root node

Hi.

I'm using the XML library to write GPS data to the GPX format.
Now i want to set the namespace of the document (attribute of root node).
when I want to set the the attirbute i always get an error that the name "xmlns" can not be redefined. Is there a way to set the namespace?

Thanks.
Thomas

Code snippet:

gpxWriter.Open (AppPath & "\" & file)
gpxWriter.WriteStartElement("gpx")
gpxWriter.WriteAttributeString("xmlns" , "http://www.topografix.com/GPX/1/1")
gpxWriter.WriteAttributeString3("xsi" , "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance","http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd")
gpxWriter.WriteEndElement
gpxWriter.Close
 

agraham

Expert
Licensed User
Longtime User
I don't understand the nuances of XML and it looks like I missed exposing a couple of overloads of WriteStartElement that deal with namespaces. I'll add them and post a new version.

Actually I have been playing with XML to try to understand it better (I didn't get far - I have a blind spot about HTML and it looks like it extends to XML as well :() and have added the .NET XmlDocument and an XmlNode objects to the library for private use so I may as well publish the library as it stands with those included. I would guess that in my ignorance I have missed important bits of these classes also! From the help of the next version -

"The XML library also provides an XmlDocument and an XmlNode objects. Between them these implement the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2. The DOM is an in-memory (cache) tree representation of an XML document and enables the creation or loading, subsequent navigation and editing of this document and its' saving. Actually these objects are thin wrappers that expose the .NET XmlDocument and XmlNode classes to Basic4ppc."
 

corwin42

Expert
Licensed User
Longtime User
Enhancements to XML Library

Hello agraham,

some time ago I expanded your XML.dll for my needs but I missed to send you the changes. I implemented the following wrapper methods to the Reader which are very useful:

- MoveToContent() - Skips whitespaces and moves to the real content of the XML file. Very handy.
- Skip() - Skips to next Element. This can speed up parsing if you don't need all Data from an Element.

Would be nice if you add them to your XML Library.

Thanks,
Markus
 

chanppc

Member
Licensed User
I try to add new node to existing XML, but can't find similar method as in .net, can anyone look at the following code & share what is the best way to achieve?

//http://www.dev102.com/2007/12/12/how-to-add-a-new-xml-node-to-file/

private void AddNodeToXMLFile(string XmlFilePath, string NodeNameToAddTo)
{
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();
//load from file
doc.Load(XmlFilePath);
//create main node
XmlNode node = doc.CreateNode(XmlNodeType.Element, "HelpButtonUrl", null);
//create the nodes first child
XmlNode ButtonName = doc.CreateElement("buttonName");
//set the value
ButtonName.InnerText = "Video Help2";
//create the nodes second child
XmlNode url = doc.CreateElement("url2");
//set the value
url.InnerText = "D:RunHelp.exe2";
// add childes to father
node.AppendChild(ButtonName);
node.AppendChild(url);
// find the node we want to add the new node to
XmlNodeList l = doc.GetElementsByTagName(NodeNameToAddTo);
// append the new node
l[0].AppendChild(node);
// save the file
doc.Save(XmlFilePath);
}
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I was hoping someone who knew more about how to use XML than I do would step in but as they haven't the attachment "sort-of" does what you wanted. It is a one-to-one translation so you can see the differences. However I don't know how to identify the target node so the last bit is just a hack to write something to the file.

For XmlReader and XmlWriter I relied upon feedback from more knowledgable users to expose what is useful. For XmlDocument and XmlNode I exposed what I thought necessary but it looks like I missed some (a lot!) important bits as I am clueless on XML. Anybody want to look at the .NET XmlDocument Members (System.Xml) and XmlNode Members (System.Xml) and tell me what I've missed that is necessary/useful.

EDIT:- See next post for updated attachment.
 
Last edited:

agraham

Expert
Licensed User
Longtime User
Here is a complete translation to Basic4ppc of the C# code above implemented by some additions to the XML library. Note that the original Xml code is appallingly wrong in the website link given, I have corrected it.

To the library I have added a XmlNodeList object and two XmlDocument.GetElementsByTagName methods. I have also exposed a previously private XmlDocument.MapXmlNodes method that returns a string showing the layout of the XmlDocument.

This is a beta library release as I am sure there is more to add to make XmlDocument really useful. Feedback please!
 

Attachments

  • NewNode2.zip
    6.1 KB · Views: 22

agraham

Expert
Licensed User
Longtime User
Version 2.2 now posted has improvements to XmlNode and XmlDocument.

A new object, XmlNodeList, is added to hold the return from ChildNodes.

XmlDocuments now has GetElementsByTagName, GetElementsByTagName2 and MapXmlNodes methods.

Both XmlDocument and XmlNode now have ChildNodes, Getchild2 and GetChild3 methods.

Both XmlDocument and XmlNode now have LocalName and NamespaceURI properties.
 

chanppc

Member
Licensed User
I've some problem to resolve the default namespace when create new node / element, I tried by extending your latest C# code but probably because I'm not good at it, I just can't make the code work when I try to include the following .net methods / properties. Hope you can incorporate them in your next release.

XmlNamespaceManager
AddNamespace

XMLElement

XmlDocument
CreateElement
DocumentElement

XmlNode
SelectSingleNode
LastChild
 

chanppc

Member
Licensed User
To be exact on my problem, there's extra xmlns="" in the 1st element I added. I modified your sample xml file to have only 1 namespace (green font) & it return similar result (red font below).

<?xml version="1.0"?>
<HelpData xmlns="http://www.w3.org/2001/XMLSchema-instance">
<helpButtonUrls>
<HelpButtonUrl>
<buttonName> BugReport </buttonName>
<url>C:BugRep.exe</url>
</HelpButtonUrl>
<HelpButtonUrl xmlns="">
<ButtonName>Video Help</ButtonName>
<url>D:RunHelp.exe</url>
</HelpButtonUrl>
</helpButtonUrls>
</HelpData>
 

agraham

Expert
Licensed User
Longtime User
I assume you want the following. I'm not very good at using XML so I don't understand why you need some of these!

XmlNamespaceManager.AddNamespace - I'll look at it

XMLElement - Isn't XmlNode enough? XMLElement is an XmlNode (as is XmlDocument), what does it do different that you need it as an explicit type? Is is access to the Attributes? If so how about an AttributeCollection object?

XmlDocument.CreateElement - Why not just use CreateNode("element", ...)?

XmlDocument. DocumentElement - This is actually FindChild("element") which isn't exposed at the moment so I'll add it.

XmlNode.SelectSingleNode - Do you want the overload that uses a NamespaceManager? Is that why you need it?

XmlNode.LastChild - Can't you get this by using ChildNodes and ChildCount and an XmlNodeList? Similarly for FirstChild.


I'm not being awkward but I need someone who understands XML to refine this library. A lot of the methods and properties seem to be duplicate ways of doing things and also there is lots of inheritance going on which doesn't map too well onto the Basic4ppc library model because the many types each need their own object in the library which leads to lots of duplication of objects and methods. I'd like to keep XmlNode as the container for elements and manipulate the attributes separately rather than having a lot of duplication in an XmlElement object. I am trying to keep this library as small as possible but without losing functionality.


<HelpButtonUrl xmlns="">
I don't see this with a single namespace, have you changed something else as well?
 

chanppc

Member
Licensed User
Thanks, I'll try out your suggestion on those method, I'm not good at XML & as you mentioned, there seems to be duplicates of methods everywhere in the .net XML.

I don't see this with a single namespace, have you changed something else as well?
I re-downloaded yr sample (post 32) & modified the input.xml to have only one namespace & without the :xsi (attached here but pls remove the .txt extension), then the result will show extra blank ns.
 
Last edited:

chanppc

Member
Licensed User
After modifying the CreateNode to 'enable' the namespaceURI parameter, I'm able to add additional node without the blank xmlns="" now :) I attached the modified NewNode.sbp file & the single ns input.xml here for reference, changes on the NewNode.sbp is that when calling CreateNode, I specify the NS.

Read from the internet that when creating new node/element, specifying the NS so it'll inherit to the child.

Original:
public System.Xml.XmlNode CreateNode(string nodetype, string name)
{
return xdoc.CreateNode(nodetype, name, null);
}

Changed:
public System.Xml.XmlNode CreateNode(string nodetype, string name, string namespaceURI)
{
return xdoc.CreateNode(nodetype, name, namespaceURI);
}
 

Attachments

  • NewNode_ns.zip
    1.2 KB · Views: 13
Last edited:

chanppc

Member
Licensed User
And we may need a way to specify the encoding, for my case, I hard-coded the below in my copies:


//XML.XmlWriter
public void Open(String filename)
{
Close();
XmlWriterSettings set = new XmlWriterSettings();
set.Indent = true;
set.Encoding = Encoding.UTF8;
wtr = System.Xml.XmlWriter.Create(filename, set);
}


//XML.XmlDocument
public void Write(string filepath)
{
XmlTextWriter xwriter = new XmlTextWriter(filepath, Encoding.UTF8);
xwriter.Formatting = Formatting.Indented;
xdoc.Save(xwriter);
xwriter.Flush();
xwriter.Close();
}
 

agraham

Expert
Licensed User
Longtime User
Version 2.3 posted.

XmlDocument changes
---------------------
Bug-fix on MapXmlNodes
CreateNode now needs a namespace parameter
Added CreateElement, CreateElement2 and CreateElement3 methods
Added DocumentElement property


XmlNode changes
-----------------
Added RemoveAttribute, AppendAttribute and PrependAttribute methods
 

chanppc

Member
Licensed User
Thanks for the updated version :)

Can I ask why you needed to do this? I thought it already wrote UTF-8 as the default and I didn't intend to support other encodings as UTF-8 seems to be preferred for XML nowadays.
I'm aware about the UTF-8 is the default. As mentioned, I'm not that good in XML so what I try to achieve is that, the xml output should be the same as the expected XML sample, in this case the sample included the encoding.

The W3Schools recommended to "Always use the encoding attribute", but if we don't intend to support other encodings, then probably just leave it for now :)
 
Top