学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

关于DOM解析XML问题

关于DOM解析XML问题

我从网上找了一个DOM解析XML的程序,程序基本理解了,就是没有测试成功,我不知道XML应该放在什么地方才可以读取,希望大家给指点一下,我把代码拿出来共享一下:

package com.lly.test;

import javax.xml.parsers.*;
import java.io.*;
import org.w3c.dom.*;
import org.xml.sax.*;
/*
* XML范例
* <?xml version="1.0" encoding="gb2312"?>
  
  <books>
  
  <book email="zhoujunhui">
  
  <name>rjzjh</name>
  
  <price>jjjjjj</price>
  
  </book>
  
  </books>
*/

public class DomParseXml
{
public DomParseXml()
{
DocumentBuilderFactory objDBF = DocumentBuilderFactory.newInstance() ;

try
{
DocumentBuilder objDB = objDBF.newDocumentBuilder();
InputStream objIS = new FileInputStream( "bin/libary.xml" );
Document objD = objDB.parse( objIS );

Element objE = objD.getDocumentElement();
NodeList objNL = objE.getChildNodes() ; // 节点的集合

if ( objNL != null )
{
for ( int i =0 ; i < objNL.getLength() ; i ++ )
{
Node objN = objNL.item( i );

if ( objN.getNodeType() == Node.ELEMENT_NODE )
{
String strEmail = objN.getAttributes().getNamedItem( "email" ).getNodeValue();
System.out.println( "email--->" + strEmail );

for ( Node objSN = objN.getFirstChild() ; objSN != null ; objSN = objSN.getNextSibling() )
{
if ( objSN.getNodeType() == Node.ELEMENT_NODE )
{
if ( objSN.getNodeName().equals( "name" ))
{
String strName = objSN.getNodeValue() ;

String strNName = objSN.getFirstChild().getNodeValue();

System.out.println( "strName--->" + strName );
System.out.println( "strNName--->" + strNName );
}

if (objSN.getNodeName().equals( "price" ))
{
String strPrice = objSN.getNodeValue() ;
System.out.println( "strPrice-->" + strPrice );
}
}
}
}
}
}
}
catch ( ParserConfigurationException e )
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(SAXException e)
{
e.printStackTrace();
}
}

public static void main(String args[])
{
new DomParseXml();
}

}

TOP

自己顶一下
今天的辛苦,为了明天的不辛苦

TOP

InputStream objIS = new FileInputStream( "bin/libary.xml" );

是读取XML的,但是我不知道这个XML应该放在什么地方?是放在TOMCAT的bin下吗?

今天的辛苦,为了明天的不辛苦

TOP

大家给帮帮忙,谢谢啦~
今天的辛苦,为了明天的不辛苦

TOP

这么久了,没有人帮我解决吗?
今天的辛苦,为了明天的不辛苦

TOP

...在这个文件同级的子目录bin下 ....相对路径的使用而已 ....

TOP

还有 ...貌似需要http://这样的格式才能正确读取到 ....

TOP

发新话题