DOM教程
刚才我10分钟就看玩了
http://resource.xml.org.cn/tutorial/ibm/x-udom/x-udom-2-3.html
刚才我10分钟就看玩了
http://resource.xml.org.cn/tutorial/ibm/x-udom/x-udom-2-3.html
解决方案 »
- JSP连接数据库不成功的原因?
- web services错误。
- 部署在resin上项目40小时内必死,大家帮我看看原因
- 求助!在自己机器上运行没有任何问题 上别人机器上就出现错误 现在依然没有解决
- Problem with CachedRowSet
- 在本机上安装了tomcat,配置之后的问题
- JSP程序怎么连接ACCESS数据库?
- java tag 标签的问题
- 求救:jsp+javaBean调用数据库(tomcat环境下)找不到Bean??感激不尽!!
- 如何在就 jBuilder6.0 下编写和运行阿???(新手问题;使用过 jbuilder工具的请进)
- (急)有关网站的定时器
- jsp使用javabean的问题,这里有代码, 初学者也不妨看看代码。
SAXParser parser = new SAXParser();
parser.setContentHandler(new PeopleHandler());
parser.parse("people.xml"); } public static void main(String[] args) throws Exception {
new XMLReader().list();
}}class PeopleHandler extends DefaultHandler { private boolean name = false;
private boolean mail = false;
private Properties property = null;
private StringBuffer sb = new StringBuffer(); public PeopleHandler() {
property = new Properties();
} public void startElement(
String nsURI,
String stripName,
String tagName,
Attributes attributes)
throws SAXException {
sb.delete(0, sb.length());
} public void characters(char[] ch, int start, int length)
throws SAXException {
sb.append(ch, start, length);
} public void endElement(String nsURI, String stripName, String tagName)
throws SAXException {
property.put(tagName, sb.toString().trim());
sb.delete(0, sb.length());
System.out.println( property.getProperty(tagName));
} public void printXML() {
Enumeration propertyNames = property.propertyNames();
while (propertyNames.hasMoreElements()) {
String propertyName = (String) propertyNames.nextElement();
System.out.println("PropertyName" + propertyName);
System.out.println("Value" + property.getProperty(propertyName));
}
}
}
<people>
<person>
<name>fantasyCoder</name>
<email>[email protected]</email>
</person>
</people>
*
*******************************************************************/import org.w3c.dom.*;public class XmlUtils{ /**
Return an Element given an XML document, tag name, and index
@param doc XML docuemnt
@param tagName a tag name
@param index a index
@return an Element
*/
public static Element getElement( Document doc , String tagName ,
int index ){
//given an XML document and a tag
//return an Element at a given index
NodeList rows = doc.getDocumentElement().getElementsByTagName(
tagName );
return (Element)rows.item( index );
} /**
Return the number of person in an XML document
@param doc XML document
@param tagName a tag name
@return the number of person in an XML document
*/
public static int getSize( Document doc , String tagName ){
//given an XML document and a tag name
//return the number of ocurances
NodeList rows = doc.getDocumentElement().getElementsByTagName(
tagName );
return rows.getLength();
} /**
Given a person element, must get the element specified
by the tagName, then must traverse that Node to get the
value.
Step1) get Element of name tagName from e
Step2) cast element to Node and then traverse it for its
non-whitespace, cr/lf value.
Step3) return it!
NOTE: Element is a subclass of Node
@param e an Element
@param tagName a tag name
@return s the value of a Node
*/
public static String getValue( Element e , String tagName ){
try{
//get node lists of a tag name from a Element
NodeList elements = e.getElementsByTagName( tagName ); Node node = elements.item( 0 );
NodeList nodes = node.getChildNodes();
//find a value whose value is non-whitespace
String s;
for( int i=0; i<nodes.getLength(); i++){
s = ((Node)nodes.item( i )).getNodeValue().trim();
if(s.equals("") || s.equals("\r")) {
continue;
}
else return s;
} }
catch(Exception ex){
System.out.println( ex );
ex.printStackTrace();
}
return null; } /**
For testing purpose, it print out Node list
@param rows a Nodelist
*/
public static void printNodeTypes( NodeList rows ){
System.out.println( "\tenumerating NodeList (of Elements):");
System.out.println( "\tClass\tNT\tNV" );
//iterate a given Node list
for( int ri = 0 ; ri < rows.getLength() ; ri++){
Node n = (Node)rows.item( ri );
if( n instanceof Element) {
System.out.print( "\tElement" );
}
else System.out.print( "\tNode" );
//print out Node type and Node value
System.out.println(
"\t"+
n.getNodeType() + "\t" +
n.getNodeValue()
);
}
System.out.println();
}
}