当前位置:首页 » 编程语言 » java的dom解析

java的dom解析

发布时间: 2022-12-27 17:28:57

Ⅰ 怎么在java中使用dom解析XML文件中标签属性值 有哪些要注意的地方

public
static
void
main(String[]
args)
{
DocumentBuilderFactory
dbf
=
DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder
db
=
dbf.newDocumentBuilder();
Document
doc
=
db.parse("d:/test.xml");
//xml文件的绝对目录
//得到根节点
Element
root
=
doc.getDocumentElement();
//通过节点的名字获得值
NodeList
nl
=
root.getElementsByTagName("HLR");
Element
e
=
(Element)
nl.item(0);
String
hlrId=e.getAttribute("HlrId");
System.out.println(hlrId);
}catch(Exception
e){
e.printStackTrace();
}
}

Ⅱ 编写一个Java程序,使用DOM解析器解析下列XML文件

用dom4j解析,非常快,具体怎么解析
SAXReader reader = new SAXReader();
String filePath = "c:/abc.xml";
File file = new File(filePath);
Document document = reader.read(file);// 读取XML文件
Element root = document.getRootElement();// 得到根节点
Element select = root.element("select");
for (Iterator i = select.nodeIterator(); i.hasNext();) {
Object obj = i.next();
String content = "";
if (obj instanceof Text) {
content = ((Text)obj).getText().replaceAll("\n", "").trim();

Ⅲ java中DOM解析XML文件奇葩的错误 java.lang.NullPointerException

代码如下:
public class LoadXml extends Activity {
private final static String TAG="LoadXml";
public static Context context = null;
Document document = null;
NodeList childsNodes = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
InputStream inputStreams = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(button1OnClickListener);
}

OnClickListener button1OnClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
getComplateXml();

}
};
public void getComplateXml(){
Log.d(TAG, "123");
try {
Log.d(TAG, "getComplateXml");
readUserConfig();
// int j = 0;
// for (int i = 0; i < childsNodes.getLength(); i++) {
// Log.d(TAG, "1");
// Node node = (Node) childsNodes.item(i);
// Log.d(TAG, "2");
// ContentResolver contentResolver = this.context
// .getContentResolver();
// Log.d(TAG, "3");
// Uri insertUri = Uri.parse("content://com.huawei.biz.LoadContentProvider/STUDENT");
// Log.d(TAG, "4");
// ContentValues values = new ContentValues();
// values.put("name", node.getNodeName());
// Log.d(TAG, node.getNodeName());
//
// values.put("id", node.getFirstChild().getNodeValue());
// Log.d(TAG, node.getFirstChild().getNodeValue());
//
// values.put("Photo",DataManager.getDrawableList().get(j));
// Log.d(TAG, DataManager.getDrawableList().get(j).toString());
// contentResolver.insert(insertUri, values);
// j++;
// }
} catch (Exception e) {
e.printStackTrace();
}

}

private void readUserConfig() throws Exception{
Log.d(TAG, "readUserConfig");
Log.d(TAG, "1");
factory = DocumentBuilderFactory.newInstance();
Log.d(TAG, "2");
builder = factory.newDocumentBuilder();
Log.d(TAG, "3");
inputStreams = LoadXml.context.getResources().getAssets().open("student.xml");
Log.d(TAG, "4");
document = builder.parse(inputStreams);
Log.d(TAG, "5");
childsNodes = document.getDocumentElement().getChildNodes();
Log.d(TAG, "6");

}
}

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:587
制作脚本网站 发布:2025-10-20 08:17:34 浏览:882
python中的init方法 发布:2025-10-20 08:17:33 浏览:575
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:761
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:678
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1006
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:251
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:108
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:799
python股票数据获取 发布:2025-10-20 07:39:44 浏览:706