xml what is xml? why do i need it? how do i use it?

21
XML What is XML? Why do I need it? How do I use it?

Upload: carol-powers

Post on 28-Dec-2015

231 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: XML What is XML? Why do I need it? How do I use it?

XML

What is XML?

Why do I need it?

How do I use it?

Page 2: XML What is XML? Why do I need it? How do I use it?

What is XML?

• XML – extensible markup language.

• A well-formed, parsable language able to convey structured textual data.

• A meta language for defining hierarchical markup languages

Page 3: XML What is XML? Why do I need it? How do I use it?

Sample XML

• <product>– <books>

• <book>– <title>Java for programmers– </title>

• </book>• <book><title>Java for morons</title></book>

– </books>

• </product>

Page 4: XML What is XML? Why do I need it? How do I use it?

How do I spec correct xml?

• You don’t have to (the parser is tolerent of bad xml)

• You can do syntax checking on parsing using– The old way DTD– The new way XML Schema (is xml for

defining xml!).

Page 5: XML What is XML? Why do I need it? How do I use it?

DTD’s

• Document Type Defn Language

• <?xml version=“1.0” encoding=“ISO=8859-1” standalone=“yes” ?>

• <!DOCTYPE nameOfItem [

• elements follow ]>

Page 6: XML What is XML? Why do I need it? How do I use it?

Doing the DOCType

<!DOCTYPE Cart [

<!ELEMENT Product (Name, price,…)>

<!ELEMENT Product (#PCDATA)>

<!ELEMENT Name (#PCDATA)>

<!ELEMENT Price (#PCDATA)>

]> <Cart> ….products </Cart>

Page 7: XML What is XML? Why do I need it? How do I use it?

Shopping Cart

<Cart><Product>

<Name> Image Processing in Java</Name>

<Price> $45.00</Price>

….more stuff

</Product>

</Cart>

Page 8: XML What is XML? Why do I need it? How do I use it?

Read XML

Build an XML reader for reading HTML!

Page 9: XML What is XML? Why do I need it? How do I use it?

writeXml

• xml.utils has:• public static void writeXml(Serializable object) {• XMLEncoder e = new XMLEncoder(• futils.Futil.getFileOutputStream("select

xml output file"));• e.writeObject(object);• e.close();• }

Page 10: XML What is XML? Why do I need it? How do I use it?

readXml

• public static Object readXml() {

• XMLDecoder e =

• new XMLDecoder(Futil.getFileInputStream("select an xml file"));

• return e.readObject();

• }

Page 11: XML What is XML? Why do I need it? How do I use it?

xml.Utils

• public static String toXml(Serializable object) {• ByteArrayOutputStream baos = new

ByteArrayOutputStream();• XMLEncoder e = new XMLEncoder(baos);• e.writeObject(object);• e.flush();• return baos.toString();• }

Page 12: XML What is XML? Why do I need it? How do I use it?

Preferences

• package: java.util.prefs.Preferences• store and retrieve user and system data. • available since jdk1.4•  putByteArray(java.lang.String key,

byte[] value) • public

 byte[] getByteArray(java.lang.String key, byte[] def)

• def=default value

Page 13: XML What is XML? Why do I need it? How do I use it?

What good is putting out bytes?

• serializable objects

• compression can be employed

• store images, audio, multi-media, etc.

Page 14: XML What is XML? Why do I need it? How do I use it?

Why would you want anything else?

• Might be good to stash XML

• Human readable text

• Good for testing/debugging

Page 15: XML What is XML? Why do I need it? How do I use it?

How do I store strings?

• void put(java.lang.String key, java.lang.String value)

•  java.lang.String get(java.lang.String key, java.lang.String def)

• def – default value

Page 16: XML What is XML? Why do I need it? How do I use it?

how do I get the user Prefs?

• static Preferences userRoot() Returns the root preference node for the calling user

Page 17: XML What is XML? Why do I need it? How do I use it?

How do I get the system prefs?

• public static Preferences systemRoot() Returns the root preference node for the system.

Page 18: XML What is XML? Why do I need it? How do I use it?

Example of getting user data

• private static String getData() {

• Preferences p = Preferences.userRoot();

• return p.get("com.docjava.foo", null);

• }

Page 19: XML What is XML? Why do I need it? How do I use it?

Example of Writing user data

• private static void putData(String s) {

• Preferences p = Preferences.userRoot();

• p.put("com.docjava.foo", s);

• }

• check futils.PreferencesExample

Page 20: XML What is XML? Why do I need it? How do I use it?

How do you set up the prefs?

Page 21: XML What is XML? Why do I need it? How do I use it?

Using the WebstartDialog

• In the security package,• use Preferences to serialize an xml version

of the webstartdialog….so that next• time you start, the old values are retained.• Use the user’s preferences.• add an xmlDecoder in the WebStartBean• The bean has the values…the dialog is the

gui.