如果在java類中擷取xml檔案裡面的標籤裡面的內容。

來源:互聯網
上載者:User

標籤:util   doc   元素   inpu   stream   his   shm   class   win   

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.javafx.sg.prism.web.NGWebView;

import sun.security.x509.KeyIdentifier;


public class getXML {
public Document getDocument(){
Document doc=null;
//xml檔案的絕對路徑。
InputStream in=Thread.currentThread().getContextClassLoader().getResourceAsStream(".xml");
try {
doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return doc;
}
public void getTags(){
Document doc=this.getDocument();
//擷取xml檔案裡<action>元素。
NodeList nodeList=doc.getElementsByTagName("action");
for(int i=0;i<nodeList.getLength();i++){
Element element=(Element) nodeList.item(i);//具體的一個action元素
//擷取每一個action元素裡面的屬性值。,完了可以封裝成一個對象存入map索引值對。
//如果<action>標籤內嵌套了其他的標籤,那麼可以繼續for迴圈來遍曆出來。
String name=element.getAttribute("name");
String className=element.getAttribute("class");
String method=element.getAttribute("method");
}
}
}

 

如果在java類中擷取xml檔案裡面的標籤裡面的內容。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.