1 / 21

XML

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>

chill
Download Presentation

XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  3. Sample XML • <product> • <books> • <book> • <title>Java for programmers • </title> • </book> • <book><title>Java for morons</title></book> • </books> • </product>

  4. 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!).

  5. DTD’s • Document Type Defn Language • <?xml version=“1.0” encoding=“ISO=8859-1” standalone=“yes” ?> • <!DOCTYPE nameOfItem [ • elements follow ]>

  6. Doing the DOCType <!DOCTYPE Cart [ <!ELEMENT Product (Name, price,…)> <!ELEMENT Product (#PCDATA)> <!ELEMENT Name (#PCDATA)> <!ELEMENT Price (#PCDATA)> ]> <Cart> ….products </Cart>

  7. Shopping Cart <Cart> <Product> <Name> Image Processing in Java</Name> <Price> $45.00</Price> ….more stuff </Product> </Cart>

  8. Read XML Build an XML reader for reading HTML!

  9. 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(); • }

  10. readXml • public static Object readXml() { • XMLDecoder e = • new XMLDecoder(Futil.getFileInputStream("select an xml file")); • return e.readObject(); • }

  11. 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(); • }

  12. 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

  13. What good is putting out bytes? • serializable objects • compression can be employed • store images, audio, multi-media, etc.

  14. Why would you want anything else? • Might be good to stash XML • Human readable text • Good for testing/debugging

  15. How do I store strings? • void put(java.lang.String key, java.lang.String value) • java.lang.Stringget(java.lang.String key, java.lang.String def) • def – default value

  16. how do I get the user Prefs? • static PreferencesuserRoot() Returns the root preference node for the calling user

  17. How do I get the system prefs? • public static PreferencessystemRoot() Returns the root preference node for the system.

  18. Example of getting user data • private static String getData() { • Preferences p = Preferences.userRoot(); • return p.get("com.docjava.foo", null); • }

  19. Example of Writing user data • private static void putData(String s) { • Preferences p = Preferences.userRoot(); • p.put("com.docjava.foo", s); • } • check futils.PreferencesExample

  20. How do you set up the prefs?

  21. 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.

More Related