- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# barcode generator library open source scala> <b ns:hi='hello'>Hello</b> in Font
scala> <b ns:hi='hello'>Hello</b> Print Data Matrix 2d Barcode In None Using Barcode creator for Font Control to generate, create Data Matrix 2d barcode image in Font applications. www.OnBarcode.comCreate Barcode In None Using Barcode creator for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comres12: scala.xml.Elem = <b ns:hi="hello">Hello</b>
Creating Code 128A In None Using Barcode creator for Font Control to generate, create Code 128 Code Set A image in Font applications. www.OnBarcode.comGenerating Data Matrix In None Using Barcode maker for Font Control to generate, create Data Matrix ECC200 image in Font applications. www.OnBarcode.comYou can assign XML to a variable: EAN / UCC - 13 Encoder In None Using Barcode encoder for Font Control to generate, create EAN / UCC - 14 image in Font applications. www.OnBarcode.comEncode GTIN - 12 In None Using Barcode encoder for Font Control to generate, create UCC - 12 image in Font applications. www.OnBarcode.comscala> val x = <b>Hello World!</b>
European Article Number 13 Creation In None Using Barcode printer for Font Control to generate, create GS1 - 13 image in Font applications. www.OnBarcode.comANSI/AIM ITF 25 Creator In None Using Barcode generator for Font Control to generate, create USS ITF 2/5 image in Font applications. www.OnBarcode.comx: scala.xml.Elem = <b>Hello World!</b>
Data Matrix Generation In Objective-C Using Barcode generation for iPhone Control to generate, create DataMatrix image in iPhone applications. www.OnBarcode.comDataMatrix Reader In C#.NET Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications. www.OnBarcode.comScala represents XML as a Seq[Node], and Node is a superclass of NodeSeq, which is a subclass of Seq[Node]. That means all the collections methods that we ve been exploring are available on XML collections including map, flatMap, filter, and foreach. This also means that XML can be used in for comprehensions. We ll explore that in the next subsection. We can define a len method that takes a Seq of any type and returns the length: Drawing USS Code 39 In Java Using Barcode printer for Android Control to generate, create USS Code 39 image in Android applications. www.OnBarcode.comBarcode Creator In VS .NET Using Barcode generation for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comscala> def len(seq: Seq[_]) = seq.length
Generate PDF417 In None Using Barcode creator for Microsoft Excel Control to generate, create PDF417 image in Office Excel applications. www.OnBarcode.comCreating Barcode In None Using Barcode generation for Microsoft Word Control to generate, create Barcode image in Microsoft Word applications. www.OnBarcode.comlen: (Seq[_])Int
Recognizing GS1 - 13 In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comRead QR Code 2d Barcode In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comCHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
Code 39 Generator In None Using Barcode maker for Online Control to generate, create Code 39 Full ASCII image in Online applications. www.OnBarcode.comUPC Code Recognizer In Visual Studio .NET Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comand call it with an XML literal: Creating GS1 - 13 In None Using Barcode maker for Word Control to generate, create EAN-13 image in Word applications. www.OnBarcode.comPDF417 Printer In VS .NET Using Barcode generator for Reporting Service Control to generate, create PDF417 image in Reporting Service applications. www.OnBarcode.comscala> len(<b>Hello</b>) res1: Int = 1
Or we can call it with the XML variable x we defined previously: scala> len(x) res2: Int = 1
Or we can call it with a List[Int]: scala> len(List(1,2,3)) res11: Int = 3
The ability to dynamically create XML in Scala is far more powerful than the ability to dynamically create Strings. Scala code can be embedded in any attribute or element body to dynamically render XML. For example, let s define a method that returns the current milliseconds as a String: scala> def now = System.currentTimeMillis.toString
now: java.lang.String
We can call the now method from the attribute definition to add a time attribute: scala> <b time={now}>Hello World</b>
res3: scala.xml.Elem = <b time="1230678511390">Hello World</b>
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
Attributes can be defined with Scala expressions of type String, NodeSeq, and Option[NodeSeq]. We did an example of String previously. Option[NodeSeq] in an attribute definition is very powerful. If the Option is None, the attribute will not be included in the resulting XML, but if the Option is Some, then the attribute will be included. An example will help illustrate. Let s define a method that tests Long for being odd: scala> def isOdd(in: Long) = in % 2L == 1L
isOdd: (Long)Boolean
Next we import Scala s XML package. XML literals are language-level, but the library that allows you to manipulate the literals must be imported.3 scala> import scala.xml._ import scala.xml._ We define the oddTime method, which will return Some[NodeSeq] if the time is odd and return None if it s even: scala> def oddTime: Option[NodeSeq] = System.currentTimeMillis match { case t if isOdd(t) => Some(Text(t.toString)) case _ => None } oddTime: Option[scala.xml.NodeSeq] And we can see that the time attribute is only included when the time is odd: scala> <b time={oddTime}>Sometimes</b>
res6: scala.xml.Elem = <b time="1230679058437">Sometimes</b>
scala> <b time={oddTime}>Sometimes</b>
3. The Class class in Java is language-level, but we must explicitly import java.lang.reflect.* because Method is in that package rather than in java.lang.*. CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
res7: scala.xml.Elem = <b time="1230679061015">Sometimes</b>
scala> <b time={oddTime}>Sometimes</b>
res8: scala.xml.Elem = <b>Sometimes</b>
You can also generate tags, text, cdata, and so on from Scala code: scala> <b>The time is {new java.util.Date}</b> res4: scala.xml.Elem = <b>The time is Tue Dec 30 15:09:09 PST 2008</b> If your embedded Scala code returns a NodeSeq, the NodeSeq will be embedded directly. If your Scala expression returns anything else, that thing will be converted to a scala.xml.Text node by calling toString on the thing. Using map, we can convert from a given type to XML that s embedded: scala> <stuff> {(1 to 3).map(i => <v id={i.toString}>#{i}</v>)} </stuff>
res0: scala.xml.Elem = <stuff> <v id="1">#1</v><v id="2">#2</v><v id="3">#3</v> </stuff>
One thing to be careful about is making sure your if expressions have an else. The type of an if expression without an else is Unit, which converts to an empty String: scala> <b>{if (true) "dogs"}</b>
res14: scala.xml.Elem = <b></b>
That s not what we expected, but this is: scala> <b>{if (true) "dogs" else ""}</b>
CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
res15: scala.xml.Elem = <b>dogs</b>
scala> <b>{if (false) "dogs" else ""}</b>
res16: scala.xml.Elem = <b></b>
Scala correctly escapes characters from your Scala expressions: scala> <b>{"Hello & Goodbye"}</b>
res3: scala.xml.Elem = <b>Hello & Goodbye</b>
scala> <b attr={"Hello < Zoo"}/>
res6: scala.xml.Elem = <b attr="Hello < Zoo"></b>
So you don t have to worry about escaping XML characters. This is generally the right thing, but if you re trying to embed a script in your XML, it might not be the right thing: scala> val info = """ var x = ""; if (true && false) alert('Woof'); """ scala> <script>{info}</script> res4: scala.xml.Elem = <script> var x = ""; if (true && false) alert('Woof'); </script> CHAPTER 3 COLLECTIONS AND THE JOY OF IMMUTABILITY
You can use PCData to embed unescaped characters in your XML: scala> <script>{PCData(info)}</script>
res5: scala.xml.Elem = <script><![CDATA[ var x = ""; if (true && false) alert('Woof'); ]]></script>
Scala s built-in XML support makes creating XML a breeze. Because the XML data structures are immutable, they can be shared and cached without concern that another thread or method may change the XML. As a side note, when I do browser-side HTML manipulation, I always have to be concerned about inserting some nodes into other nodes because the insertion causes parent node references to be changed. This means that if I had inserted the nodes elsewhere, they ll be unceremoniously removed from the old place to be inserted into the new place. This means I have to copy the entire node structure each time I do an insert, which is an O(n) operation. With Scala s immutable XML data structures, I only have to change from the insertion point to the root node, an O(log n) operation. Now that we ve seen how to create XML in Scala, let s see how to parse and manipulate XML.
|
|