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

}
}

熱點內容
內置存儲卡可以拆嗎 發布:2025-05-18 04:16:35 瀏覽:336
編譯原理課時設置 發布:2025-05-18 04:13:28 瀏覽:378
linux中進入ip地址伺服器 發布:2025-05-18 04:11:21 瀏覽:612
java用什麼軟體寫 發布:2025-05-18 03:56:19 瀏覽:32
linux配置vim編譯c 發布:2025-05-18 03:55:07 瀏覽:107
砸百鬼腳本 發布:2025-05-18 03:53:34 瀏覽:944
安卓手機如何拍視頻和蘋果一樣 發布:2025-05-18 03:40:47 瀏覽:741
為什麼安卓手機連不上蘋果7熱點 發布:2025-05-18 03:40:13 瀏覽:803
網卡訪問 發布:2025-05-18 03:35:04 瀏覽:511
接收和發送伺服器地址 發布:2025-05-18 03:33:48 瀏覽:372