<coin> <value>0.5</value> <name>half dollar</name> </coin>
<img src="hamster.jpeg"/>
<img src="hamster.jpeg" width="400" height="300"/>
<?xml version 1.0?>
<?xml version 1.0?> <purse> more data </purse>
<elementTag optional attributes> contents </elementTag> or <elementTag optional attributes/>
<p>Use XML for <strong>robust</strong> data formats.</p>
<a href="http://java.sun.com"> ... </a>
<value currency="USD">0.5</value> or <value currency="EUR">0.5</value>
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder();
String fileName = . . . ; File f = new File(filename); Document doc = builder.parse(f);
String urlName = . . . ; URL u = new URL(urlName); Document doc = builder.parse(u);
InputStream in = . . . ; Document doc = builder.parse(in);
<?xml version="1.0"?>
<items> <item> <product> <description>Ink Jet Refill Kit</description> <price>29.95</price> </product> <quantity>8</quantity> </item> <item> <product> <description>4-port Mini Hub</description> <price>19.95</price> </product> <quantity>4</quantity> </item> </items>
Element root = doc.getDocumentElement();
NodeList nodes = root.getChildNodes(); int i = . . . ; //a value between o and getlength() - 1 Node child = nodes.item(i);
Element priceElement = . . . ; String name = priceElement.getTagName();
String attributeValue = priceElement.getAttribute("currency")
Element priceNode = . . . ; Text priceData = (Text)priceNode.getFirstChild(); String priceString = priceNode.getData(); double price = Double.parseDouble(priceString);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.newDocument(); //empty document
Element itemElement = doc.createElement("item");
Text quantityText= doc.createTextNode("8");
priceElement.setAttribute("currency", "USD");
// create elements Element itemElement = doc.createElement("item"); Element productElement = doc.createElement("product"); Element descriptionElement = doc.createElement("description"); Element priceElement = doc.createElement("price"); Element quantityElement = doc.createElement("quantity"); Text descriptionText = doc.createTextNode("Ink Jet Refill Kit"); Text priceText = doct.createTextNode("29.95"); Text quantityText = doc.createTextNode("8"); // add elements to the document doc.appendChild(itemElement); itemElement.appendChild(productElement); itemElement.appendChild(quantityElement); productElement.appendChild(descriptionElement); productElement.appendChild(priceElement); descriptionElement.appendChild(descriptionText); priceElement.appendChild(priceText); quantityElement.appendChild(quantityText);
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(doc), new StreamResult(System.out));
<!ELEMENT items (item*)>
<! ELEMENT item (product, quantity)>
<! ELEMENT product (description, price)>
<!ELEMENT quantity (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT price (#PCDATA)>
<!ELEMENT items (item)*>
<!ELEMENT item (product, quantity)>
<!ELEMENT product (description, price)>
<!ELEMENT quantity (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST Element Attribute Type Default>
<!ATTLIST price currency (USD | EUR | JPY ) #REQUIRED >
<!ATTLIST price currency CDATA #IMPLIED >
<!ATTLIST price currency CDATA "USD" >
<!DOCTYPE rootElement [ rules ]>
<?xml version="1.0"?> <!DOCTYPE items [ <!ELEMENT items (item*)> <!ELEMENT item (product, quantity)> <!ELEMENT product (description, price)> <!ELEMENT quantity (#PCDATA)> <!ELEMENT description (#PCDATA)> <!ELEMENT price (#PCDATA)> ]> <items> <item> <product> <description>Ink Jet Refill Kit</description> <price>29.95</price> </product> <quantity>8</quantity> </item> <item> <product> <description>4-port Mini Hub</description> <price>19.95</price> </product> <quantity>4</quantity> </item> </items>
<!DOCTYPE items SYSTEM "items.dtd" >
<!DOCTYPE items SYSTEM "http://www.mycompany.com/dtds/items.dtd">
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(. . .);
factory.setValidating(true); factory.setIgnoringElementContentWhitespace(true);