B4A Library [lib] XOM

warwound

Expert
Licensed User
This is a partial implementation of the XOM library by by Elliotte Rusty Harold.

XOM™ is a new XML object model. It is an open source (LGPL), tree-based API for processing XML with Java that strives for correctness, simplicity, and performance, in that order.
The library implements all XOM read methods, it's also possible to write XML using this library but not all XOM write methods have (so far) been implemented.

So with B4A we have the existing XmlSax parser which is what's know as a push parser.
XmlSax will parse an XML document from start to end and generate an event each time it finds a start or end element.

XOM let's you access an XML document using DOM methods.
You can access any elements in any order with no requirement to parse the entire document first.
The library has abstract objects that represent various XML elements - you work with objects such as XOMAttribute and XOMNode.

A code example explains it better:

B4X:
Sub Process_Globals
End Sub 

Sub Globals
   Dim CurrentCondition As XOMElement
   Dim DateSpinner As Spinner
   Dim MinTempLabel As Label
   Dim MaxTempLabel As Label
   Dim PrecipitationLabel As Label
   Dim WeatherDescLabel As Label
   Dim WeatherElements As XOMElements
   Dim WindDirectionLabel As Label
   Dim WindSpeedLabel As Label
End Sub 

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   
   Dim XOMBuilder1 As XOMBuilder
   XOMBuilder1.Initialize("XOMBuilder1")
   
   '   the BuildFromString method is used here as the BuildFromFile method cannot access files in assets
   Dim XmlString As String=File.GetText(File.DirAssets, "weather.xml")
   XOMBuilder1.BuildFromString(XmlString, "", Null)
   
End Sub 

Sub Activity_Resume
End Sub 

Sub Activity_Pause (UserClosed As Boolean)
End Sub 

Sub DateSpinner_ItemClick (Position As Int, Value As Object)
   If Position=0 Then
     MaxTempLabel.Text="Temp: "&CurrentCondition.GetFirstChildElementByName("temp_C").Value&"C"
     MinTempLabel.Text="Time: "&CurrentCondition.GetFirstChildElementByName("observation_time").Value
     PrecipitationLabel.Text="Precipitation: "&CurrentCondition.GetFirstChildElementByName("precipMM").Value&"mm"
     WeatherDescLabel.Text="Summary: "&CurrentCondition.GetFirstChildElementByName("weatherDesc").Value
     WindDirectionLabel.Text="Wind direction: "&CurrentCondition.GetFirstChildElementByName("winddir16Point").Value
     WindSpeedLabel.Text="Wind speed: "&CurrentCondition.GetFirstChildElementByName("windspeedMiles").Value&"mph"
   Else
     Dim SelectedWeatherElement As XOMElement
     SelectedWeatherElement=WeatherElements.GetElement(Position-1)
     
     MaxTempLabel.Text="Max temp: "&SelectedWeatherElement.GetFirstChildElementByName("tempMaxC").Value&"C"
     MinTempLabel.Text="Min temp: "&SelectedWeatherElement.GetFirstChildElementByName("tempMinC").Value&"C"
     PrecipitationLabel.Text="Precipitation: "&SelectedWeatherElement.GetFirstChildElementByName("precipMM").Value&"mm"
     WeatherDescLabel.Text="Summary: "&SelectedWeatherElement.GetFirstChildElementByName("weatherDesc").Value
     WindDirectionLabel.Text="Wind direction: "&SelectedWeatherElement.GetFirstChildElementByName("winddirection").Value
     WindSpeedLabel.Text="Wind speed: "&SelectedWeatherElement.GetFirstChildElementByName("windspeedMiles").Value&"mph"
   End If
End Sub 

Sub XOMBuilder1_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
   If XOMDocument1.IsInitialized Then
     Log("XOMDocument is initialized")
     Dim RootElement As XOMElement
     RootElement=XOMDocument1.RootElement
     
     DateSpinner.Add("Current conditions")
     CurrentCondition=RootElement.GetFirstChildElementByName("current_condition")
     
     WeatherElements=RootElement.GetChildElementsByName("weather")
     Dim i, WeatherElementsCount As Int
     WeatherElementsCount=WeatherElements.Size
     For i=0 To WeatherElementsCount-1
       DateSpinner.Add(WeatherElements.GetElement(i).GetFirstChildElementByName("date").Value)
     Next
     
     DateSpinner.Enabled=True
     DateSpinner_ItemClick(0, Null)
   Else
     '   XOMDocument1 will be uninitialized if an error has occurred
     Log("An error has occured and the XOMDocument has NOT been initialized")
     Log(LastException.Message)
   End If
End Sub
The example creates an XOMDocument from a String obtained from a file in Assets.
It extracts the 'current_condition' element and then the 'weather' elements.
These elements are saved as Global XOMElement and XOMElements objects, the rest of the XOMDocument is discarded.

XOM can build an XOMDocument from File, InputStream and Url as well as from a String.
Use of the BuildFromURL method requires android.permission.INTERNET.
XOM does NOT automatically enable this permission, so if you use BuildFromUrl you must ensure that your application has this permission enabled.
I decided this was better than automatically enabling the internet permission - if you do not use BuildFromUrl then your application will not enable an unnecessary permission.


Use of XOM requires two additional jar files to be added to your B4A additional libraries folder: xom-1.2.10.jar and dtd-xercesImpl.jar, the forum attachment size limit prevents me from attaching these two files to this post so i have made them available from here.

I am not actively developing this library, it's an old project that i have uploaded to enable another forum member to extract data from an HTML webpage.
I'll fix any bugs but do not plan to implement any new features.
Source code is available on request if any other forum members want to further develop or modify the library.

Martin.
 

Attachments

Last edited:

warwound

Expert
Licensed User
Here's the XOM library reference:

XOM
Comment:
The B4A XOM library is a partial implementation of the java XOM library by Elliotte Rusty Harold.
The java XOM library is licensed under the terms and conditions of the LGPL, http://www.gnu.org/licenses/lgpl.html
More info can be found here: http://www.xom.nu/
Version: 1.20
  • XOMAttribute
    Fields:
    • ATTRIBUTE_TYPE_CDATA As Type
    • ATTRIBUTE_TYPE_ENTITIES As Type
    • ATTRIBUTE_TYPE_ENTITY As Type
    • ATTRIBUTE_TYPE_ENUMERATION As Type
    • ATTRIBUTE_TYPE_ID As Type
    • ATTRIBUTE_TYPE_IDREF As Type
    • ATTRIBUTE_TYPE_IDREFS As Type
    • ATTRIBUTE_TYPE_NMTOKEN As Type
    • ATTRIBUTE_TYPE_NMTOKENS As Type
    • ATTRIBUTE_TYPE_NOTATION As Type
    • ATTRIBUTE_TYPE_UNDECLARED As Type
    Methods:
    • Copy As XOMNode
      Creates a deep copy of this attribute that is not attached to an element.
    • Initialize (LocalName As String, Value As String)
      Creates a new attribute in no namespace with the specified name and value and undeclared type.
    • Initialize2 (LocalName As String, Value As String, AttributeType1 As Type)
      Creates a new attribute in no namespace with the specified name, value, and type.
    • Initialize3 (LocalName As String, Uri As String, Value As String)
      Creates a new attribute in the specified namespace with the specified name and value and undeclared type.
    • Initialize4 (LocalName As String, Uri As String, Value As String, AttributeType1 As Type)
      Creates a new attribute in the specified namespace with the specified name, value, and type.
    • Initialize5 (Attribute1 As Attribute)
      Creates a new attribute which is a deep copy of Attribute1.
    • IsInitialized As Boolean
    • SetNamespace (Prefix As String, URI As String)
      Set the attribute's namespace prefix and URI.
    • ToString As String
      Returns a string representation of the attribute suitable for debugging and diagnosis.
    • ToXML As String
      Returns a string representation of the attribute that is a well-formed XML attribute.
    Properties:
    • LocalName As String
      Get or Set the local name of this attribute, not including the prefix.
    • NameSpacePrefix As String [read only]
      Get the prefix of this attribute, or the empty string if this attribute is not in a namespace.
    • NamespaceURI As String [read only]
      Get the namespace URI of this attribute, or the empty string if this attribute is not in a namespace.
    • QualifiedName As String [read only]
      Get the qualified name of this attribute, including the prefix if this attribute is in a namespace.
    • Type As Type
      Get or Set the DTD type of this attribute.
    • Value As String
      Get or Set the attribute value.
  • XOMBuilder
    Events:
    • BuildDone (XOMDocument1 As XOMDocument, Tag As Object)
    Methods:
    • BuildFromFile (FilePath As String, Tag As Object)
      Build an XOMDocument from the file specified by FilePath.
      Tag - Pass any Object as the Tag and when the method has completed, the Tag Object is returned in the BuildDone event.
    • BuildFromInputStream (InputStream1 As InputStream, BaseUri As String, AutoCloseStream As Boolean, Tag As Object)
      Build an XOMDocument from InputStream1.
      Tag - Pass any Object as the Tag and when the method has completed, the Tag Object is returned in the BuildDone event.
    • BuildFromString (XmlString As String, BaseUri As String, Tag As Object)
      Build an XOMDocument from the XmlString.
      Tag - Pass any Object as the Tag and when the method has completed, the Tag Object is returned in the BuildDone event.
    • BuildFromURL (Url As String, Tag As Object)
      Build an XOMDocument from the document specified by Url.
      Tag - Pass any Object as the Tag and when the method has completed, the Tag Object is returned in the BuildDone event.
    • Initialize (EventName As String)
    • IsInitialized As Boolean
  • XOMComment
    Methods:
    • Copy As XOMNode
    • Initialize (Data As String)
      Creates a new comment node from string data.
      A comment node cannot have any child nodes.
      It can be a child of an Element or a Document.
    • IsInitialized As Boolean
    • ToString As String
    • ToXML As String
    Properties:
    • Value As String
      Get or Set the content of this comment.
  • XOMDocType
    Methods:
    • Initialize (RootElementName As String)
      Creates a new document type declaration with no public or system ID.
    • Initialize2 (RootElementName As String, SystemId As String)
      Creates a new document type declaration with a system ID but no public ID.
    • Initialize3 (RootElementName As String, SystemId As String, PublicId As String)
      Creates a new document type declaration with a public ID and a system ID.
    • IsInitialized As Boolean
    • ToString As String
      Returns a string form of the DocType suitable for debugging and diagnosis.
    • ToXML As String
      Returns a string containing the actual XML form of the document type declaration represented by this object.
    Properties:
    • InternalDTDSubset As String
      Get or Set the internal DTD subset; that is the part of the DTD between [ and ].
    • PublicID As String
      Get or Set the public ID for the external DTD subset.
    • RootElementName As String
      Get or Set the name the document type declaration specifies for the root element.
    • SystemID As String
      Get or Set the system ID for the external DTD subset.
  • XOMDocument
    Methods:
    • Copy As XOMNode
    • Initialize (RootElement As Element)
      Creates a new Document object with the specified root element.
    • IsInitialized As Boolean
    • RemoveChild (Position As Int) As XOMNode
    • RemoveChild2 (Node1 As Node) As XOMNode
    • ReplaceChild (OldChild As Node, NewChild As Node)
    • ToString As String
    • ToXML As String
    Properties:
    • BaseURI As String
      Get or Set the URI from which this document was loaded, and against which relative URLs in this document will be resolved.
    • DocType As XOMDocType
      Get or Set this document's document type declaration.
    • RootElement As XOMElement
      Get or Set the document's current root element.
    • Value As String [read only]
      Returns the value of the document as defined by XPath 1.0.
  • XOMElement
    Methods:
    • AddAttribute (Attribute1 As Attribute)
    • AddNamespaceDeclaration (Prefix As String, Uri As String)
    • AppendChild (Text As String)
      Converts a string to a text node and appends that node to the children of this node.
    • Copy As XOMNode
    • GetAttributeByIndex (Index As Int) As XOMAttribute
    • GetAttributeByName (Name As String) As XOMAttribute
    • GetAttributeByNameAndNamespace (Name As String, Namespace As String) As XOMAttribute
    • GetAttributeValueByName (Name As String) As String
    • GetAttributeValueByNameAndNamespace (Name As String, Namespace As String) As String
    • GetChildElements As XOMElements
    • GetChildElementsByName (Name As String) As XOMElements
    • GetChildElementsByNameAndNamespace (Name As String, Namespace As String) As XOMElements
    • GetFirstChildElementByName (Name As String) As XOMElement
    • GetFirstChildElementByNameAndNamespace (Name As String, Namespace As String) As XOMElement
    • GetNamespacePrefix As String
      Returns the prefix of this element, or the empty string if this element does not have a prefix.
    • GetNamespacePrefix2 (Index As Int) As String
      Returns the index'th namespace prefix declared on this element.
    • GetNamespaceURI As String
      Returns the namespace URI of this element, or the empty string if this element is not in a namespace.
    • Initialize (Name As String)
      Creates a new element in no namespace.
    • Initialize2 (Name As String, Uri As String)
      Creates a new element in a namespace.
    • Initialize3 (Element1 As Element)
      Creates a new element which is a deep copy of Element1.
    • InsertChild (Text As String, Index As Int)
    • IsInitialized As Boolean
    • RemoveAttribute (Attribute1 As Attribute) As XOMAttribute
    • RemoveChildren As XOMNodes
    • RemoveNamespaceDeclaration (Prefix As String)
      Removes the mapping of the specified prefix.
    • SetNamespacePrefix (NamespacePrefix As String)
      Sets the namespace prefix of this element.
    • SetNamespaceURI (NamespaceURI As String)
      Sets the namespace URI of this element.
    • ToString As String
    • ToXML As String
    Properties:
    • AttributeCount As Int [read only]
    • BaseURI As String
      Get or Set the URI from which this element was loaded, and against which relative URLs in this element will be resolved, unless an xml:base attribute overrides this.
    • LocalName As String
      Get or Set the local name of this element, not including the namespace prefix or colon.
    • NamespaceDeclarationCount As Int [read only]
      Get the number of namespace declarations on this element.
    • QualifiedName As String [read only]
      Get the complete name of this element, including the namespace prefix if this element has one.
    • Value As String [read only]
  • XOMElements
    Methods:
    • GetElement (Index As Int) As XOMElement
    • IsInitialized As Boolean
    • Size As Int
  • XOMNamespace
    Methods:
    • Copy As XOMNode
    • Detach
    • GetChild (Index As Int) As XOMNode
    • IsInitialized As Boolean
    • ToString As String
    • ToXML As String
    Properties:
    • ChildCount As Int [read only]
    • Prefix As String [read only]
    • Value As String [read only]
  • XOMNode
    Methods:
    • Copy As XOMNode
    • Detach
    • GetChild (Index As Int) As XOMNode
    • HashCode As Int
    • IsInitialized As Boolean
    • Query (XPath As String) As XOMNodes
      Returns the nodes selected by the XPath expression in the context of this node in document order as defined by XSLT.
    • ToXML As String
    Properties:
    • BaseURI As String [read only]
    • ChildCount As Int [read only]
    • Document As XOMDocument [read only]
    • Parent As XOMParentNode [read only]
    • Value As String [read only]
  • XOMNodes
    Methods:
    • AppendNode (Node1 As Node)
    • ContainsNode (Node1 As Node) As Boolean
    • GetNode (Index As Int) As XOMNode
    • Initialize
      Creates an empty node list.
    • InsertNode (Node1 As Node, Index As Int)
    • IsInitialized As Boolean
    • RemoveNode (Index As Int) As XOMNode
    • Size As Int
  • XOMParentNode
    Methods:
    • AppendChild (Node1 As Node)
    • GetChild (index As Int) As XOMNode
    • GetChildCount As Int
    • IndexOf (Node1 As Node) As Int
    • InsertChild (Node1 As Node, Index As Int)
    • IsInitialized As Boolean
    • RemoveChild (Index As Int) As XOMNode
    • RemoveChild2 (Node1 As Node) As XOMNode
    • ReplaceChild (OldChild As Node, NewChild As Node)
    • SetBaseURI (Uri As String)
      Sets the URI against which relative URIs in this node will be resolved.
  • XOMProcessingInstruction
    Methods:
    • Copy As XOMNode
    • IsInitialized As Boolean
    • ToString As String
    • ToXML As String
    Properties:
    • Target As String
    • Value As String
  • XOMSerializer
    Methods:
    • Flush
      Flushes the data onto the output stream.
    • Initialize (OutputStream1 As OutputStream)
      Create a new serializer that uses the UTF-8 encoding.
    • Initialize2 (OutputStream1 As OutputStream, Encoding As String)
      Create a new serializer that uses the specified encoding.
    • IsInitialized As Boolean
    • SetOutputStream (OutputStream1 As OutputStream)
      Flushes the previous output stream and redirects further output to the new output stream.
    • Write (Document1 As Document)
      Serializes Document1 onto the output stream using the current options.
    Properties:
    • Encoding As String [read only]
      Get the name of the character encoding used by this serializer.
    • Indent As Int
      Get or Set the number of spaces this serializer indents.
    • LineSeparator As String
      Get or Set the string used as a line separator.
    • MaxLength As Int
      Get or Set the preferred maximum line length.
    • PreserveBaseURI As Boolean
      Get or Set whether this serializer inserts extra xml:base attributes to attempt to preserve base URI information from the document.
    • UnicodeNormalizationFormC As Boolean
      Get or Set whether serialization will perform Unicode normalization on all data using normalization form C (NFC).
  • XOMText
    Methods:
    • Copy As XOMNode
    • GetChild (Index As Int) As XOMNode
    • Initialize (Data As String)
      Creates a new Text object with the value of Data.
    • IsInitialized As Boolean
    • ToString As String
    • ToXML As String
    Properties:
    • ChildCount As Int [read only]
    • Value As String [read only]
      Returns the XPath 1.0 string-value of this Text node.

Martin.
 
Last edited:

keirS

Well-Known Member
Licensed User
Interesting library. I have been playing round with the JSoup HTML parser library to extract data from HTML tables. The library structures look very similar.
 

warwound

Expert
Licensed User
XOM library updated to version 1.01

This update adds no methods or features.
If an exception occurs within the XOMBuilder when it is executing of it's various BuildFrom??? methods then it will set the exception as the B4A LastException.

So if a BuildFrom??? method fails and your BuildDone event is passed Null instead of an XOMDocument you can use the B4A LastException keyword to debug the problem.

Version 1.01 is attached to the first post in this thread.

Martin.
 

bartv

Member
Licensed User
The XOM.xml file contains the tag
HTML:
    <dependsOn>xom-1.2.8</dependsOn>
Which causes the example to bail out with an error :
HTML:
---------------------------
Basic4android
---------------------------
An error occurred.
 
A referenced library is missing: xom-1.2.8
---------------------------
OK
---------------------------
What should I do to correct this?
 
Last edited:

DSD

Member
Licensed User
I'm having some difficulty with reading a xml-file.
The xml-file in itself is very simple, it consists of one element with a few attributes.

Here's an example:

<xml-user-list ver="1.0">
<user name="User1" password="1234" profiles="" enabled="true"/>
<user name="User2" password="1234" profiles="" enabled="true"/>
</xml-user-list>

I've based my code on the working sample above.

I've also checked that the xml-file is correct and that all the elements are read into the DOM and that each element contains attributes.

I keep getting an exception: "RunTimeException object should first be initialized (XOMAttribute)"

I don't think I should initialize the attribute, surely the XOMBuilder should do this, because I can see that the element contain several attributes.

This row is causing the exception:
B4X:
lvd.Name=UserElements.GetElement(i).GetAttributeByName("name").Value
Below is the complete code-section:

B4X:
Sub xom_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
    If XOMDocument1=Null Then
        '    XOMDocument1 will be Null if an error has occurred
        Log("An error has occured and the XOMDocument has NOT been created")
    Else
        Log("XOMDocument is NOT Null")
        Dim RootElement As XOMElement
        RootElement=XOMDocument1.RootElement
      
        UserElements=RootElement.GetChildElementsByName("user")
        Dim i, UserElementsCount As Int
        UserElementsCount=UserElements.Size
      
        For i=0 To UserElementsCount-1
            Dim lvd As ListViewData
          
            lvd.Name=UserElements.GetElement(i).GetAttributeByName("name").Value
            lvd.Password=UserElements.GetElement(i).GetAttributeByName("password").Value
            lvd.Enabled=UserElements.GetElement(i).GetAttributeByName("enabled").Value
          
            AddUser(lvd)
        Next
      
    End If
End Sub
Any help is appreciated.

/Mattias
 

warwound

Expert
Licensed User
@DSD

GetAttributeByName will return an uninitialized XOMAttribute if it fails to find the attribute.

The problem is i think related to the first element of your xml: <xml-user-list ver="1.0">

The validator here says "This Document is not valid.", "Can not find declaration of element 'xml-user-list'.".
Whereas the validator here says the xml is valid.

Another thing to note is i made a change to the library and didn't have time (!) to update the code example.
If the creation of the XOMDocument fails then BuildDone is passed an uninitialized XOMDocument not a Null value.
So update your code as below and see if the XOMDocument is initialized:

B4X:
Sub xom_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
  If XOMDocument1.IsInitialized Then
  Log("XOMDocument is initialized")
  Dim RootElement As XOMElement
  RootElement=XOMDocument1.RootElement
   
  UserElements=RootElement.GetChildElementsByName("user")
  Dim i, UserElementsCount As Int
  UserElementsCount=UserElements.Size
   
  For i=0 To UserElementsCount-1
  Dim lvd As ListViewData
   
  lvd.Name=UserElements.GetElement(i).GetAttributeByName("name").Value
  lvd.Password=UserElements.GetElement(i).GetAttributeByName("password").Value
  lvd.Enabled=UserElements.GetElement(i).GetAttributeByName("enabled").Value
   
  AddUser(lvd)
  Next
  Else
  '  XOMDocument1 will not be initialized if an error has occurred
  Log("An error has occured and the XOMDocument has NOT been initialized")
  Log(LastException.Message)
  End If
End Sub
Martin.
 

DSD

Member
Licensed User
@DSD

GetAttributeByName will return an uninitialized XOMAttribute if it fails to find the attribute.

The problem is i think related to the first element of your xml: <xml-user-list ver="1.0">

The validator here says "This Document is not valid.", "Can not find declaration of element 'xml-user-list'.".
Whereas the validator here says the xml is valid.

Another thing to note is i made a change to the library and didn't have time (!) to update the code example.
If the creation of the XOMDocument fails then BuildDone is passed an uninitialized XOMDocument not a Null value.
So update your code as below and see if the XOMDocument is initialized:

B4X:
Sub xom_BuildDone(XOMDocument1 As XOMDocument, Tag As Object)
  If XOMDocument1.IsInitialized Then
  Log("XOMDocument is initialized")
  Dim RootElement As XOMElement
  RootElement=XOMDocument1.RootElement
  
  UserElements=RootElement.GetChildElementsByName("user")
  Dim i, UserElementsCount As Int
  UserElementsCount=UserElements.Size
  
  For i=0 To UserElementsCount-1
  Dim lvd As ListViewData
  
  lvd.Name=UserElements.GetElement(i).GetAttributeByName("name").Value
  lvd.Password=UserElements.GetElement(i).GetAttributeByName("password").Value
  lvd.Enabled=UserElements.GetElement(i).GetAttributeByName("enabled").Value
  
  AddUser(lvd)
  Next
  Else
  '  XOMDocument1 will not be initialized if an error has occurred
  Log("An error has occured and the XOMDocument has NOT been initialized")
  Log(LastException.Message)
  End If
End Sub
Martin.
Thanks for helping me. :)
Actually I think the xml-file is correct but I was using the GetAttributesByName and when I used GetAttributeValueByName it worked...

Do you know how I can check for existing attributes, some attributes are missing on some elements.
I would like to check if an attribute is present, how would I go about that?

Thanks in advance.
 

warwound

Expert
Licensed User
XOM library updated to version 1.20

This updates makes the b4a library compatible with the latest version of the java XOM library.
The latest version of the java XOM library is version 1.2.10 and you will need to download and update to xom-1.2.10.jar, available along dtd-xercesImpl.jar from here: b4a.martinpearman.co.uk/xom/b4a_xom_additional_library_files.zip.

As well as making the b4a library compatible with the latest java library i have implemented a few more methods in some of the objects.
These are methods mainly related to authoring XML documents and will be of no interest if you are simply reading XML documents.

I've updated the reference in post #2.

I've also updated the demo project to better demonstrate how to detect if the XOMBuilder object's BuildDone event passes a valid XOMDocument to the event handler.

Version 1.20 is attached to post #1 in this thread.

Martin.
 

warwound

Expert
Licensed User
Thanks for helping me. :)
Actually I think the xml-file is correct but I was using the GetAttributesByName and when I used GetAttributeValueByName it worked...

Do you know how I can check for existing attributes, some attributes are missing on some elements.
I would like to check if an attribute is present, how would I go about that?

Thanks in advance.
You need to do something like this:

B4X:
Dim XOMAttribute1 as XOMAttribute
XOMAttribute1=UserElements.GetElement(i).GetAttributeByName("name")
If XOMAttribute1.IsInitialized Then
  ' the attribute "name" exists
  lvd.Name=XOMAttribute1.Value
Else
  ' the attribute "name" does not exist
End If
Martin.
 
  • Like
Reactions: DSD

andrewj

Active Member
Licensed User
Hi Martin,
Another life-saver. I've just written a bookmark import/export routine in about 10 lines of code which would be a nightmare in the SAX model.
One suggestion for the future, which would make the code even more streamlined. Could you make the collection objects of a form which can be iterated with For..Each? e.g.
B4X:
For each oBookmark in oRoot.GetChildElementsByName("Bookmark")
That would mean even fewer changes porting from VB.NET.

Just a suggestion. Thanks again!
Andrew
 

Indy

Active Member
Licensed User
Hi,

I read in one of the post that using this library it is possible to initialise without the need for events. I would like to use this in a code module so can't use any events. How can I use this to read through a simple xml file and extract values from tags?

Thanks.
 

rboeck

Well-Known Member
Licensed User
I had the question, if the library is usable with b4j and found out the answer with converted demo project: yes, it works!
Included a demo in B4J, but because is almost the same i have posted it here..
 

Attachments

Last edited:

warwound

Expert
Licensed User
@rboeck

I don't have time to put together a new thread for usage with b4j.
You might as well add your b4j demo code to this thread where it'll be easy for others to find.
 

Valeriy Lakhtin

Member
Licensed User
XOM library updated to version 1.01

This update adds no methods or features.
If an exception occurs within the XOMBuilder when it is executing of it's various BuildFrom??? methods then it will set the exception as the B4A LastException.

So if a BuildFrom??? method fails and your BuildDone event is passed Null instead of an XOMDocument you can use the B4A LastException keyword to debug the problem.

Version 1.01 is attached to the first post in this thread.

Martin.
I have problems with initializing XOM document.
B4X:
Dim XOMBuilder1 As XOMBuilder 
XOMBuilder1.Initialize("XOMBuilder1") 
XOMBuilder1.BuildFromString(XmlString, "", Null)
I checked my XML file, there are no errors, he successfully read in XmlString, but then i have problem in ...
B4X:
Sub XOMBuilder1_BuildDone(XOMDoc As XOMDocument, Tag As Object)
If XOMDoc.IsInitialized Then
line XOMDoc.IsInitialized gives the value FALSE

Please advise what can be the causes to this problem
 
Top