[ Pobierz całość w formacie PDF ]

{
Document doc = null;
try
{
DOMParser parser = new DOMParser();
parser.parse(uri);
doc = parser.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
// We've parsed the document now, so let's sort it and print it.
if (doc != null)
{
sortLines(doc);
printDOMTree(doc);
}
}
public String getTextFromLine(Node lineElement)
{
StringBuffer returnString = new StringBuffer();
if (lineElement.getNodeName().equals("line"))
{
NodeList kids = lineElement.getChildNodes();
if (kids != null)
{
if (kids.item(0).getNodeType() == Node.TEXT_NODE)
{
returnString.append(kids.item(0).getNodeValue());
}
}
}
51
Appendix  Listings of our samples Tutorial  XML Programming in Java
else
returnString.setLength(0);
return new String(returnString);
}
/** Sorts the elements in the file.
It uses a bubble sort algorithm, since a
sonnet only has 14 lines. **/
public void sortLines(Document doc)
{
NodeList theLines = doc.getDocumentElement().
getElementsByTagName("line");
if (theLines != null)
{
int len = theLines.getLength();
for (int i = 0; i
for (int j = 0; j
if (getTextFromLine(theLines.item(j)).
compareTo(getTextFromLine(theLines.item(j+1))) > 0)
theLines.item(j).getParentNode().
insertBefore(theLines.item(j+1),
theLines.item(j));
}
}
/** Prints the specified node, recursively. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
System.out.println("");
printDOMTree(((Document)node).getDocumentElement());
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
System.out.print("
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
}
System.out.println(">");
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i
printDOMTree(children.item(i));
}
break;
52
Tutorial  XML Programming in Java Appendix  Listings of our samples
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName());
System.out.print(";");
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE:
{
System.out.print("
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
// print text
case Node.TEXT_NODE:
{
if (node.getNodeValue().trim().length() > 0)
System.out.print(node.getNodeValue());
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE:
{
System.out.print("
System.out.print(node.getNodeName());
String data = node.getNodeValue();
if (data != null && data.length() > 0)
{
System.out.print(" ");
System.out.print(data);
}
System.out.print("?>");
break;
}
}
if (type == Node.ELEMENT_NODE)
{
System.out.println();
System.out.print("
System.out.print(node.getNodeName());
System.out.print('>');
}
}
/** Main program entry point. */
public static void main(String argv[])
{
if (argv.length == 0)
{
System.out.println("Usage: java domSorter uri");
System.out.println(" where uri is the URI of the XML document you want to
sort.");
System.out.println(" Sample: java domSorter sonnet.xml");
System.out.println();
53
Appendix  Listings of our samples Tutorial  XML Programming in Java
System.out.println(" Note: Your XML document must use the sonnet DTD.");
System.exit(1);
}
domSorter ds = new domSorter();
ds.parseAndSortLines(argv[0]);
}
}
domTwo.java
This code is identical to domOne.java, except it uses Sun s XML parser instead of IBM s. It illustrates
the portability of the DOM interfaces.
/*
* (C) Copyright IBM Corp. 1999 All rights reserved.
*
* US Government Users Restricted Rights Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*
* The program is provided "as is" without any warranty express or
* implied, including the warranty of non-infringement and the implied
* warranties of merchantibility and fitness for a particular purpose.
* IBM will not be liable for any damages suffered by you as a result
* of using the Program. In no event will IBM be liable for any
* special, indirect or consequential damages or lost profits even if
* IBM has been advised of the possibility of their occurrence. IBM
* will not be liable for any third party claims against you.
*/
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.sun.xml.parser.Parser;
import com.sun.xml.tree.XmlDocumentBuilder;
/**
* domTwo.java
* Illustrates how to go through a DOM tree. Identical to domOne,
* except it uses Sun s XML parser instead of IBM s.
*/
public class domTwo
{
public void parseAndPrint(String uri)
{
Document doc = null;
try
{
XmlDocumentBuilder builder = new XmlDocumentBuilder();
Parser parser = new com.sun.xml.parser.Parser();
parser.setDocumentHandler(builder);
builder.setParser(parser);
builder.setDisableNamespaces(false);
54
Tutorial  XML Programming in Java Appendix  Listings of our samples
parser.parse(uri);
doc = builder.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
// We've parsed the document now, so let's print it.
if (doc != null)
printDOMTree(doc);
}
/** Prints the specified node, recursively. */
public void printDOMTree(Node node)
{
int type = node.getNodeType();
switch (type)
{
// print the document element
case Node.DOCUMENT_NODE:
{
System.out.println("");
printDOMTree(((Document)node).getDocumentElement());
break;
}
// print element with attributes
case Node.ELEMENT_NODE:
{
System.out.print("
System.out.print(node.getNodeName());
NamedNodeMap attrs = node.getAttributes();
for (int i = 0; i
{
Node attr = attrs.item(i);
System.out.print(" " + attr.getNodeName() +
"=\"" + attr.getNodeValue() +
"\"");
}
System.out.println(">");
NodeList children = node.getChildNodes();
if (children != null)
{
int len = children.getLength();
for (int i = 0; i
printDOMTree(children.item(i));
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE:
{
System.out.print("&");
System.out.print(node.getNodeName()); [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • cukierek.xlx.pl