當前位置:首頁 » 編程語言 » 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");

}
}

熱點內容
linuxsnmp安裝 發布:2025-07-02 23:07:08 瀏覽:649
北理c語言答案 發布:2025-07-02 23:05:57 瀏覽:304
sql同比 發布:2025-07-02 23:03:39 瀏覽:835
一個伺服器獲取多個ip 發布:2025-07-02 23:02:43 瀏覽:786
三星電腦wifi怎麼連接wifi密碼 發布:2025-07-02 22:55:00 瀏覽:712
安卓開發選哪個版本的SDK 發布:2025-07-02 22:19:07 瀏覽:486
未上傳圖片 發布:2025-07-02 22:14:01 瀏覽:599
安卓qq直播怎麼分享聲音 發布:2025-07-02 22:13:21 瀏覽:250
安卓系統怎麼刷機清除內存 發布:2025-07-02 22:08:19 瀏覽:697
安卓手機6位數密碼有多少組 發布:2025-07-02 21:50:31 瀏覽:798