String reg = "(\\<\\w+\\:Extract\\>)\\d+(\\<\\/\\w+\\:Extract\\>)";
String str = "<p:Sample>Sample</p:Sample> <p:Extract>100</p:Extract> <p2:Other>0</p2:Other> <p:Extract>200</p:Extract>";
Systemoutprintln (strreplaceAll (reg, "$1222$2"));
java读取xml节点元素,主要使用java提供的解析xml的工具类SAXParserFactory,如下代码:
package xmlxmlreader;import javaioFile;
import javanetURL;
import javautilProperties;
import javaxxmlparsersSAXParser;
import javaxxmlparsersSAXParserFactory;
public class CFGParser {//解析xml文件的工具类
private Properties props;
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
thisprops = props;
}
public void parse(String filename) throws Exception
{
CFGHandler handler = new CFGHandler();
SAXParserFactory factory = SAXParserFactorynewInstance();
factorysetNamespaceAware(false);
factorysetValidating(false);
SAXParser parser = factorynewSAXParser();
URL confURL = supergetClass()getClassLoader()getResource(filename);
if (confURL == null) {
Systemoutprintln("Can't find configration file");
return;
}
try
{
parserparse(confURLtoString(), handler);
thisprops = handlergetProps();
}
finally {
factory = null;
parser = null;
handler = null;
}
}
public void parseFile(String filename)
throws Exception
{
CFGHandler handler = new CFGHandler();
SAXParserFactory factory = SAXParserFactorynewInstance();
factorysetNamespaceAware(false);
factorysetValidating(false);
SAXParser parser = factorynewSAXParser();
File f = new File(filename);
if ((f == null) || (!fexists()))
return;
try
{
parserparse(f, handler);
thisprops = handlergetProps();
}
finally {
factory = null;
parser = null;
handler = null;
}
}
}
package xmlxmlreader;
import javautilProperties;
import orgxmlsaxAttributes;
import orgxmlsaxSAXException;
import orgxmlsaxhelpersDefaultHandler;
public class CFGHandler extends DefaultHandler
{
private Properties props;
private String currentSet;
private String currentName;
private StringBuffer currentValue = new StringBuffer();
public CFGHandler()
{
thisprops = new Properties();
}
public Properties getProps() {
return thisprops;
}
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException
{
thiscurrentValuedelete(0, thiscurrentValuelength());
thiscurrentName = qName;
}
public void characters(char[] ch, int start, int length) throws SAXException
{
thiscurrentValueappend(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException
{
thispropsput(qNametoLowerCase(), thiscurrentValuetoString()trim());
}
}
xml文件
<xml version="10" 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="javautil" pageEncoding="UTF-8"%>
<html>
<jsp:useBean id="cfgp" scope="page" class="xmlxmlreaderCFGParser"></jsp:useBean>
<body>
<%
cfgpparse("kaoqinxml");
Properties pro = cfgpgetProps();
String stTime = progetProperty("morningbegin");
String edTime = progetProperty("morningend");
String afternoonbegin = progetProperty("afternoonbegin");
outprintln(stTime+"\n"+edTime+"\n"+afternoonbegin);
Systemoutprintln(stTime+"\n"+edTime+"\n"+afternoonbegin);
%>
</body>
</html>
以上就是关于java 正则表达式替换XML中某个节点的内容,求正则表达式(在线等)全部的内容,包括:java 正则表达式替换XML中某个节点的内容,求正则表达式(在线等)、java如何读取xml节点元素值、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)