📄 domrepresentation.java
字号:
/*
* WebLogic Server Unleashed
*
*/
package com.wlsunleashed.xml.xpath;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JFrame;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import weblogic.apache.xerces.parsers.DOMParser;
import weblogic.xml.xpath.DOMXPath;
import weblogic.xml.xpath.XPathException;
/**
* This class uses the WebLogic specific DOMXPath to search for items in an
* XML DOM Document.
*
* @version 1.0
*/
public class DOMRepresentation extends JFrame {
/** The Document that has to be searched */
private Document document;
/**
* Creates a new DOMRepresentation object.
*
* @param filename The e-mail XML file.
*
* @throws SAXException Thrown when any SAX parsing goes wrong.
* @throws IOException Thrown when the file is not found.
*/
public DOMRepresentation(String filename)
throws SAXException, IOException {
document = parseDocument(filename);
}
/**
* The entry point into this application
*
* @param args All the command line parameters
*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage DOMRepresentation <Any XML file>.xml");
System.exit(1);
}
try {
DOMRepresentation anObj = new DOMRepresentation(args[0]);
System.out.println(" Number of Recipients = "
+ anObj.numRecipients());
anObj.listRecipients();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Searches the XML document for all "to" and "cc" addresses and prints
* them out.
*
* @exception XPathException Thrown on any XPath search exception
*/
private void listRecipients()
throws XPathException {
DOMXPath recipientsXPath = new DOMXPath("email/to");
Set recipients = recipientsXPath.evaluateAsNodeset(document);
Iterator iter = recipients.iterator();
System.out.println("This email is being addressed to");
while (iter.hasNext()) {
Node aNode = (Node) iter.next();
System.out.println("* "
+ aNode.getAttributes().getNamedItem("name")
.getNodeValue());
}
recipientsXPath = new DOMXPath("email/cc");
recipients = recipientsXPath.evaluateAsNodeset(document);
iter = recipients.iterator();
System.out.println("This email is being copied to");
while (iter.hasNext()) {
Node aNode = (Node) iter.next();
System.out.println("* "
+ aNode.getAttributes().getNamedItem("name")
.getNodeValue());
}
}
/**
* Returns the number of recipients (to addresses) in the email
*
* @return double
*
* @exception XPathException Thrown on any XPath search exception
*/
private double numRecipients()
throws XPathException {
DOMXPath numRecipientsXPath = new DOMXPath("count(email/to)");
return numRecipientsXPath.evaluateAsNumber(document);
}
/**
* Method parseDocument. Parses the document and displays it on a tree
*
* @param xmlFile The file name
*
* @return Document
*
* @throws IOException When an IO exception occurs
* @throws SAXException When an error occurs while parsing.
*/
private Document parseDocument(String xmlFile)
throws IOException, SAXException {
DOMParser aParser = new DOMParser();
aParser.parse(xmlFile);
return aParser.getDocument();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -