xml what is xml? why do i need it? how do i use it?
TRANSCRIPT
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
Sample XML
• <product>– <books>
• <book>– <title>Java for programmers– </title>
• </book>• <book><title>Java for morons</title></book>
– </books>
• </product>
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!).
DTD’s
• Document Type Defn Language
• <?xml version=“1.0” encoding=“ISO=8859-1” standalone=“yes” ?>
• <!DOCTYPE nameOfItem [
• elements follow ]>
Doing the DOCType
<!DOCTYPE Cart [
<!ELEMENT Product (Name, price,…)>
<!ELEMENT Product (#PCDATA)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
]> <Cart> ….products </Cart>
Shopping Cart
<Cart><Product>
<Name> Image Processing in Java</Name>
<Price> $45.00</Price>
….more stuff
</Product>
</Cart>
Read XML
Build an XML reader for reading HTML!
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();• }
readXml
• public static Object readXml() {
• XMLDecoder e =
• new XMLDecoder(Futil.getFileInputStream("select an xml file"));
• return e.readObject();
• }
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();• }
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
What good is putting out bytes?
• serializable objects
• compression can be employed
• store images, audio, multi-media, etc.
Why would you want anything else?
• Might be good to stash XML
• Human readable text
• Good for testing/debugging
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
how do I get the user Prefs?
• static Preferences userRoot() Returns the root preference node for the calling user
How do I get the system prefs?
• public static Preferences systemRoot() Returns the root preference node for the system.
Example of getting user data
• private static String getData() {
• Preferences p = Preferences.userRoot();
• return p.get("com.docjava.foo", null);
• }
Example of Writing user data
• private static void putData(String s) {
• Preferences p = Preferences.userRoot();
• p.put("com.docjava.foo", s);
• }
• check futils.PreferencesExample
How do you set up the prefs?
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.