Attribute value of XML node in Scala -
suppose writing function (node, string) => option[string] attribute value attribute name given node.
def getattributevalue(node: node, attributename: string): option[string] = { val txt = (node \ ("@" + attributename)).text if (txt.isempty) none else some(txt) } does make sense ? how fix/improve ?
scala has methods attribute defined on node class, return option[seq[node]], requires further processing string.
so i'm using similar in code:
implicit class xmlenhancements(node: node) { def attributeopt(attribute: string): option[string] = node.attribute(attribute) flatmap (_.headoption) map (_.text) } also, scala defines method def \@(attributename: string): string on xml.node class, believe it's ok define attributeopt under \@? alias:
implicit class xmlenhancements(node: node) { def \@?(attribute: string): option[string] = node \@ attribute match { case "" => none case s => some(s) } }
Comments
Post a Comment