本文目錄一覽:
java中如何讀入XML格式的string或硬盤文件?還有怎麼判定一個string的內容為XML格式的?
public class Test {
public static void main(String args[]){
boolean b = false;
try {
FileReader read = new FileReader(“D:/struts.xml”);
BufferedReader br = new BufferedReader(read);
String row;
int i=0;
while((row = br.readLine())!=null){
System.out.println(row);
if(row.substring(0, 5).equals(“?xml”) row.substring(row.length()-2, row.length()).equals(“?”)){
b=true;
}
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
if(b){
System.out.println(“這是一個xml文件”);
}else
System.out.println(“這不是一個xml文件”);
}
}
運行結果:
?xml version=”1.0″ encoding=”UTF-8″?
!DOCTYPE struts PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
“”
struts
include file=”struts-login.xml” /
include file=”struts-okd.xml” /
include file=”struts-smt.xml” /
/struts
這是一個xml文件
—————————————————-
當我把文件換為test.xml時,運行結果:
01211132abcd
這不是一個xml文件
java讀取xml文件內容
java中不是有個讀取xml文件的類嗎?之間調用那類讀取出來,然後用取節點的方法去取對應節點的里的值。等下給你代碼。
public class ReaderXml {
private static String filename = “E:\\workplace\\readerxml\\bin\\reader\\xml\\reader.xml”;
// private static Config config;
public static void main(String []args) throws Exception{
//這裡用反射機制
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder=domfac.newDocumentBuilder();
//讀取文件流
InputStream is=new FileInputStream(filename);
Document doc=dombuilder.parse(is);
Element root=doc.getDocumentElement();
//獲取所有xml節點
NodeList dbinfo=root.getChildNodes();
if(dbinfo!=null){
for(int i=0;idbinfo.getLength();i++){
//獲取節點判斷
Node db=dbinfo.item(i);
//如果是Hardwares節點,也就是你xml文件的最頂處的節點
if(db.getNodeName().equals(“Hardwares”)){
//獲取第二個節點包含的所有節點
NodeList list=db.getChildNodes();
for(int y=0;ylist.getLength();y++){
Node n=list.item(y);
//如果節點等於Hardware
if(n.getNodeName().equals(“Hardware”)){
//獲取Hardware節點中的所有節點
NodeList CnodeList=n.getChildNodes();
//取出Hardware裏面的所有節點
for(int k=0;kCnodeList.getLength();k++){
//取出節點
Node cn=CnodeList.item(k);
//去掉裏面的#text文件節點。沒用,這個不是你配置的節點,應該是xml文件隱藏的
if(!cn.getNodeName().equals(“#text”)){
//打印你所配置的所有節點 System.out.println(“node[“+k+”]=”+cn.getNodeName()+” nodeValue[“+k+”]=”+cn.getTextContent());
}
}
}
}
}
}
}
}
}
//具體你要幹嘛自己弄了!
在Java中如何讀取XML字符串的元素值
java讀取xml節點元素,主要使用java提供的解析xml的工具類SAXParserFactory,如下代碼:
package xml.xmlreader;import java.io.File;import java.net.URL;import java.util.Properties;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;public class CFGParser {//解析xml文件的工具類 private Properties props; public Properties getProps() { return props; } public void setProps(Properties props) { this.props = props; } public void parse(String filename) throws Exception { CFGHandler handler = new CFGHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); SAXParser parser = factory.newSAXParser(); URL confURL = super.getClass().getClassLoader().getResource(filename); if (confURL == null) { System.out.println(“Can’t find configration file.”); return; } try { parser.parse(confURL.toString(), handler); this.props = handler.getProps(); } finally { factory = null; parser = null; handler = null; } } public void parseFile(String filename) throws Exception { CFGHandler handler = new CFGHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); SAXParser parser = factory.newSAXParser(); File f = new File(filename); if ((f == null) || (!f.exists())) return; try { parser.parse(f, handler); this.props = handler.getProps(); } finally { factory = null; parser = null; handler = null; } }}package xml.xmlreader;import java.util.Properties;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler; public class CFGHandler extends DefaultHandler{ private Properties props; private String currentSet; private String currentName; private StringBuffer currentValue = new StringBuffer(); public CFGHandler() { this.props = new Properties(); } public Properties getProps() { return this.props; } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { this.currentValue.delete(0, this.currentValue.length()); this.currentName = qName; } public void characters(char[] ch, int start, int length) throws SAXException { this.currentValue.append(ch, start, length); } public void endElement(String uri, String localName, String qName) throws SAXException { this.props.put(qName.toLowerCase(), this.currentValue.toString().trim()); }}xml文件 ?xml version=”1.0″ encoding=”UTF-8″?xml-body refresh_userlist desc=”用戶列表刷新間隔時間(秒)”6/refresh_userlist refresh_message desc=”短消息刷新間隔時間(秒)”10/refresh_message morningbegin desc=”上午上班時間”23:00/morningbegin morningend desc=”上午下班時間”12:00/morningend afternoonbegin desc=”下午上班時間”18:00/afternoonbegin/xml-bodyjsp獲取各個節點的值:%@ page language=”java” import=”java.util.*” pageEncoding=”UTF-8″%html jsp:useBean id=”cfgp” scope=”page” class=”xml.xmlreader.CFGParser”/jsp:useBean body % cfgp.parse(“kaoqin.xml”); Properties pro = cfgp.getProps(); String stTime = pro.getProperty(“morningbegin”); String edTime = pro.getProperty(“morningend”); String afternoonbegin = pro.getProperty(“afternoonbegin”); out.println(stTime+”\n”+edTime+”\n”+afternoonbegin); System.out.println(stTime+”\n”+edTime+”\n”+afternoonbegin); % /body/html
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/237156.html