導航:首頁 > 編程語言 > javaxml工具類

javaxml工具類

發布時間:2022-11-02 18:08:01

A. java xml如何寫

這個是要求命令行程序吧
首先是選擇一個xml庫,然後是實現基本的集合查詢,最後是實現輸入輸出

B. xml在java項目中起到的作用具體是什麼

java項目中,xml文件一般都是用來存儲一些配置信息
一般的編程, 多數用來存儲配置信息 . 拿JDBC來說,可以把資料庫連接字元串寫到xml,如果要修改數據源,只需要改xml就可以了,沒必要再去重新編譯java文件,而且,這些配置信息放在一起,別的人來讀你寫的代碼的時候,就方便了很多
框架中的xml , 除了配置信息 , 還可以寫一些對應關系,其實也是一種配置信息 .拿struts來說,xml配置的是頁面url對應後台java類(action)的關系,在配置和修改的時候,只需要改一個xml文件就可以了,沒必要一個個的查找java代碼
java項目完成之後,每個模塊應該都是獨立的,模塊之間的關系都可以使用xml來進行維護,spring就是這樣的一個框架

一個好的項目,需要有良好的可拓展性,如果把所有的邏輯關系還有配置信息都寫入代碼中,會使程序的可拓展性變差,為了解決這個問題,xml就可以對整個項目進行調度(spring)

還有使用xml作為數據儲存,不過用起來很少,多數還是用來存放配置信息

C. java 解析xml重要嗎 在工作中用的多嗎 面試時會不會問到

xml在Java編程中還是挺重要的,不過一般的面試官不會問這類的問題,面試官考核的還是看個人的邏輯能力,xml的編程是需要在實際過程中實踐的。

當然不同的面試官問的問題也不一樣,這個不能一概而論。

望採納~~

D. java 怎麼解析xml數據包數據 將xml各節點下的數據取出來

xml解析方法可以說有4種工具;
jdom,dom,sax,一般比較好懂的就是jdom,你網路jdom就有很多,網路文庫也可以

E. 在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-body>jsp獲取各個節點的值:<%@ 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>

F. JAVA調第三方介面返回XML文件用httpclient實現求大神給指示(新號就5分全給了)要一個工具類,一個用例

以下是一個http介面調用的例子:區別是返回的json數據,xml數據也是一樣的,解析下xml數據就可以了,希望對你有幫助
public String getOrderCount() {
String mobile = ServletActionContext.getRequest().getParameter("mobile");
String urlStr = "http://127.0.0.1/OderServer/orderCount.htm";
urlStr += "?telphone=" + mobile + "&endTime=" + DateUtil.getCurrentDay("yyyyMMdd") + "&timeLength=2";

URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL(urlStr);
// 以post方式請求
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();

// 獲取響應代碼
//int code = httpurlconnection.getResponseCode();
//System.out.println("code " + code);

// 獲取頁面內容
java.io.InputStream in = httpurlconnection.getInputStream();
java.io.BufferedReader breader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
StringBuffer result = new StringBuffer();
String str = breader.readLine();
while (str != null) {
result.append(str);
str = breader.readLine();
}
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/json;charset=utf-8");
response.getWriter().print(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
return null;
}

G. java怎麼讀寫xml文件

public List<Student> doReadXML(String path) throws Exception {
List<Student> empvoList = new ArrayList<Student>();

File file = new File(path);
//輸入流對象
FileInputStream fis = new FileInputStream(file);
//jdom解析器
SAXBuilder sb = new SAXBuilder();
Document doc= sb.build(fis);
//獲得XML的根元素
Element root = doc.getRootElement();
//獲得根元素下的所有子元素
List<Element> employees = root.getChildren();
for(int i=0;i<employees.size();i++){
Element employee =employees.get(i);
Student stu= new Student();
String name = employee.getChildText("name");
String sex = employee.getChildText("sex");
String agetemp = employee.getChildText("age");
String home = employee.getChildText("home");
String email = employee.getChildText("email");

stu.setName(name);
stu.setSex(sex);
int age = 0;
if(agetemp.equals("")){
age = 0;
} else {
age = Integer.parseInt(agetemp);
}
stu.setAge(age);
stu.setHome(home);
stu.setEmail(email);
System.out.println(name+"\t"+i);
empvoList.add(stu);
}
return empvoList;
}

H. java如何讀取xml節點元素值

java讀取xml節點元素,主要使用java提供的解析xml的工具類SAXParserFactory,如下代碼:

packagexml.xmlreader;
importjava.io.File;
importjava.net.URL;
importjava.util.Properties;
importjavax.xml.parsers.SAXParser;
importjavax.xml.parsers.SAXParserFactory;
publicclassCFGParser{//解析xml文件的工具類
privatePropertiesprops;

publicPropertiesgetProps(){
returnprops;
}
publicvoidsetProps(Propertiesprops){
this.props=props;
}

publicvoidparse(Stringfilename)throwsException
{
CFGHandlerhandler=newCFGHandler();

SAXParserFactoryfactory=SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);

SAXParserparser=factory.newSAXParser();

URLconfURL=super.getClass().getClassLoader().getResource(filename);
if(confURL==null){
System.out.println("Can'tfindconfigrationfile.");
return;
}
try
{
parser.parse(confURL.toString(),handler);
this.props=handler.getProps();
}
finally{
factory=null;
parser=null;
handler=null;
}
}

publicvoidparseFile(Stringfilename)
throwsException
{
CFGHandlerhandler=newCFGHandler();

SAXParserFactoryfactory=SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParserparser=factory.newSAXParser();


Filef=newFile(filename);
if((f==null)||(!f.exists()))
return;
try
{
parser.parse(f,handler);


this.props=handler.getProps();
}
finally{
factory=null;
parser=null;
handler=null;
}
}
}
packagexml.xmlreader;
importjava.util.Properties;
importorg.xml.sax.Attributes;
importorg.xml.sax.SAXException;
importorg.xml.sax.helpers.DefaultHandler;


{
privatePropertiesprops;
privateStringcurrentSet;
privateStringcurrentName;
=newStringBuffer();

publicCFGHandler()
{
this.props=newProperties();
}

publicPropertiesgetProps(){
returnthis.props;
}

publicvoidstartElement(Stringuri,StringlocalName,StringqName,Attributesattributes)
throwsSAXException
{
this.currentValue.delete(0,this.currentValue.length());
this.currentName=qName;
}

publicvoidcharacters(char[]ch,intstart,intlength)throwsSAXException
{
this.currentValue.append(ch,start,length);
}

publicvoidendElement(Stringuri,StringlocalName,StringqName)
throwsSAXException
{
this.props.put(qName.toLowerCase(),this.currentValue.toString().trim());
}
}
xml文件


<?xmlversion="1.0"encoding="UTF-8"?>
<xml-body>
<refresh_userlistdesc="用戶列表刷新間隔時間(秒)">6</refresh_userlist>
<refresh_messagedesc="短消息刷新間隔時間(秒)">10</refresh_message>
<morningbegindesc="上午上班時間">23:00</morningbegin>
<morningenddesc="上午下班時間">12:00</morningend>
<afternoonbegindesc="下午上班時間">18:00</afternoonbegin>
</xml-body>
jsp獲取各個節點的值:
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<html>
<jsp:useBeanid="cfgp"scope="page"class="xml.xmlreader.CFGParser"></jsp:useBean>
<body>
<%
cfgp.parse("kaoqin.xml");
Propertiespro=cfgp.getProps();
StringstTime=pro.getProperty("morningbegin");
StringedTime=pro.getProperty("morningend");
Stringafternoonbegin=pro.getProperty("afternoonbegin");

out.println(stTime+" "+edTime+" "+afternoonbegin);
System.out.println(stTime+" "+edTime+" "+afternoonbegin);
%>
</body>
</html>

I. java中dom4j解析xml文件怎麼獲取節點屬性

dom4j中,使用Element.attributes方法可以獲取到節點的屬性,而使用elements則可以獲取相應的子節點
比如:
Element root = doc.getRootElement();
List attrList = root.attributes();
for (int i = 0; i < attrList.size(); i++) {
//屬性的取得
Attribute item = (Attribute)attrList.get(i);
System.out.println(item.getName() + "=" + item.getValue());
}
List childList = root.elements();
for (int i = 0; i < childList.size(); i++) {
//子節點的操作
Element it = (Element) childList.get(i);
//對子節點進行其它操作...
}

J. Java xml遍歷

java xml遍歷主要是使用java提供工具類DocumentBuilderFactory
類來解析xml文件和遍歷,如下代碼:

importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.DocumentBuilder;
importorg.w3c.dom.*;
importjava.io.File;
publicclassTraverseXML
{
publicstaticvoidmain(String[]args)
{
try{
Stringfile="TestData\test.xml";
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
DocumentBuilderdocBuilder=dbf.newDocumentBuilder();
Documentdoc=docBuilder.parse(newFile(file));

NodeListnodes=doc.getElementsByTagName("person");
System.out.println("總共有"+nodes.getLength()+"個人。");

for(inti=0;i<nodes.getLength();i++)
{
Nodenode=nodes.item(i);
NodeListchildNodes=node.getChildNodes();
System.out.println("person有"+childNodes.getLength()+"個節點。");

for(intj=0;j<childNodes.getLength();j++)
{
NodechildNode=childNodes.item(j);
if(childNode.getNodeType()==Node.ELEMENT_NODE&&childNode.getNodeName().equals("name"))
System.out.println("名字:"+childNode.getFirstChild().getNodeValue());
if(childNode.getNodeType()==Node.ELEMENT_NODE&&childNode.getNodeName().equals("age"))
System.out.println("年齡:"+childNode.getFirstChild().getNodeValue());
}
System.out.println();
}
}catch(Exceptione){
e.printStackTrace();
}
}
}
閱讀全文

與javaxml工具類相關的資料

熱點內容
自動解壓失敗叫我聯系客服 瀏覽:482
易語言新手源碼 瀏覽:456
oa伺服器必須有固定ip地址 瀏覽:42
傳奇源碼分析是什麼 瀏覽:267
解放壓縮機支架 瀏覽:255
程序員禿頂搞笑相遇 瀏覽:6
IBM手機app商店叫什麼名字 瀏覽:834
jpeg壓縮質量 瀏覽:774
雲伺服器評測對比 瀏覽:145
java日期轉string 瀏覽:221
openfire源碼編譯 瀏覽:897
在線小工具箱引流網站源碼 瀏覽:337
非科班程序員自學 瀏覽:800
壓縮泡沫鞋底底材 瀏覽:219
程序員職場第一課2正確的溝通 瀏覽:679
遇到不合法app應該怎麼辦 瀏覽:91
匯編程序編譯後的文件 瀏覽:79
大智慧均線源碼 瀏覽:373
單片機排阻的作用 瀏覽:216
滴滴金融app被下架如何還款 瀏覽:212