points.add(pt);// Add the point to the list of points.}}All the nested<point>elements are children of the<pointlist>element. Theifstatementin this code fragment is necessary because an element can have other children in addition to itsnested elements. In this example, we only want to process the children that are elements.All these techniques can be employed to write the file input method for the sample programSimplePaintWithXML.java. When building the data structure represented by an XML file, myapproach is to start with a default data structure and then to modify and add to it as I traversethe DOM representation of the file. It’s not a trivial process, but I hope that you can follow it:Color newBackground = Color.WHITE;ArrayList<CurveData> newCurves = new ArrayList<CurveData>();Element rootElement = xmldoc.getDocumentElement();if ( ! rootElement.getNodeName().equals("simplepaint") )throw new Exception("File is not a SimplePaint file.");String version = rootElement.getAttribute("version");try {double versionNumber = Double.parseDouble(version);if (versionNumber > 1.0)throw new Exception("File requires a newer version of SimplePaint.");}catch (NumberFormatException e) {}NodeList nodes = rootElement.getChildNodes();for (int i = 0; i < nodes.getLength(); i++) {if (nodes.item(i) instanceof Element) {Element element = (Element)nodes.item(i);if (element.getTagName().equals("background")) { // Read background color.int r = Integer.parseInt(element.getAttribute("red"));int g = Integer.parseInt(element.getAttribute("green"));int b = Integer.parseInt(element.getAttribute("blue"));newBackground = new Color(r,g,b);}else if (element.getTagName().equals("curve")) { // Read data for a curve.
CHAPTER 11. STREAMS, FILES, AND NETWORKING587CurveData curve = new CurveData();curve.color = Color.BLACK;curve.points = new ArrayList<Point>();newCurves.add(curve); // Add this curve to the new list of curves.NodeList curveNodes = element.getChildNodes();for (int j = 0; j < curveNodes.getLength(); j++) {if (curveNodes.item(j) instanceof Element) {Element curveElement = (Element)curveNodes.item(j);if (curveElement.getTagName().equals("color")) {int r = Integer.parseInt(curveElement.getAttribute("red"));int g = Integer.parseInt(curveElement.getAttribute("green"));int b = Integer.parseInt(curveElement.getAttribute("blue"));curve.color = new Color(r,g,b);}else if (curveElement.getTagName().equals("point")) {int x = Integer.parseInt(curveElement.getAttribute("x"));int y = Integer.parseInt(curveElement.getAttribute("y"));curve.points.add(new Point(x,y));}else if (curveElement.getTagName().equals("symmetric")) {String content = curveElement.getTextContent();if (content.equals("true"))curve.symmetric = true;}}}}}}curves = newCurves; // Change picture in window to show the data from file.setBackground(newBackground);repaint();You can find the complete source code inSimplePaintWithXML.java.∗ ∗ ∗XML has developed into an extremely important technology, and some applications of it arevery complex. But there is a core of simple ideas that can be easily applied in Java. Knowingjust the basics, you can make good use of XML in your own Java programs.
Exercises588Exercises for Chapter 111.The sample programDirectoryList.java
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 755 pages?
Upload your study docs or become a
Course Hero member to access this document