导航:首页 > 编程语言 > javaxmltohtml

javaxmltohtml

发布时间:2023-06-07 00:23:34

java怎么解析xml文件

Stringxml="<xml><ToUserName><![CDATA[toUser]]></ToUserName>"冲举简
+"<FromUserName><![CDATA[fromUser]]>答拆</FromUserName>"
+"<CreateTime>12345678</CreateTime>"
+"<MsgType><![CDATA[text]]></MsgType>"
+"<Content>散裤<![CDATA[你好]]></Content></xml>";
try{
//加载xml字符串
org.dom4j.Documentdocument=org.dom4j.DocumentHelper.parseText(xml);
//获取根节点
org.dom4j.Elementroot=document.getRootElement();
//获取值==toUser
StringtoUserName=root.elementText("ToUserName");
System.out.println(toUserName);
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}

⑵ 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>

⑶ 如何用java代码创建xml文件

用java自带的就可以,有问题可以问我

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//构造
public XMLUtil(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}

/**
* 保存到文件
*/
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();。

⑷ 如何用Java实现对xml文件的读取和写入以及保存

直接附源码import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;import org.dom4j.*;
import org.dom4j.io.XMLWriter;
public class Dom4jSample { public static void main(String[] args) {
Dom4jSample dom4jSample = new Dom4jSample();
Document document = dom4jSample.createDocument();
try{
dom4jSample.FileWrite(document);

Document documentStr = dom4jSample.StringToXML("<China>I Love!</China>");
dom4jSample.XMLWrite(documentStr);

Element legend = dom4jSample.FindElement(document);
System.out.println(legend.getText());
}
catch(Exception e)
{

}
}

/*
* Create a XML Document
*/
public Document createDocument()
{
Document document = DocumentHelper.createDocument();

Element root = document.addElement("root");

Element author1 = root.addElement("Lynch");
author1.addAttribute("Age","25");
author1.addAttribute("Country","China");
author1.addText("I am great!");

Element author2 = root.addElement("Legend");
author2.addAttribute("Age","25");
author2.addAttribute("Country","China");
author2.addText("I am great!too!");

return document;
}

/*
* Create a XML document through String
*/
public Document StringToXML(String str) throws DocumentException
{
Document document = DocumentHelper.parseText(str);
return document;
}
public Element FindElement(Document document)
{
Element root = document.getRootElement();
Element legend = null;
for(Iterator i=root.elementIterator("legend");i.hasNext();)
{
legend = (Element)i.next();
}
return legend;
}

/*
* Write a XML file
*/
public void FileWrite(Document document) throws IOException
{
FileWriter out = new FileWriter("C:/Dom2jSample.xml");
document.write(out);
out.close();
}

/*
* Write a XML format file
*/
public void XMLWrite(Document document) throws IOException
{
XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStr.xml"));
writer.write(document);
writer.close();
}
}

⑸ java如何组装xml报文

可以用 我给你的方式组合xml报文

阅读全文

与javaxmltohtml相关的资料

热点内容
用手机编程用什么软件 浏览:915
看岛国片网址 浏览:420
男主叫叶天的小说 浏览:378
jdk目录下的文件夹 浏览:951
好看的带颜色的现代小说 浏览:699
怎么看app什么时候更新 浏览:220
各银行的官方app是什么 浏览:782
吴孟达买花给盲女的电影 浏览:471
你若安好便是晴天pdf 浏览:940
app上续保车险用什么方式付款 浏览:480
wps如何把pdf转成PPT 浏览:63
以太网pdf 浏览:194
泰国剧罪孽父亲出海 浏览:7
叶天明柳韵txt 浏览:825
bch主流算法 浏览:767
免费在线国产片 浏览:200
苹果平板为什么连接服务器错误 浏览:89
穿越东晋成为王凝之 浏览:958
手机里的文件夹怎么拉出来 浏览:281