⑴ java的xml的解析方式有什么,他们的解析流程是怎么样的,有什么区别
DOM4J,JDOM,SAX
public class DomTest3
{
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("student.xml"));
//获得根元素结点
Element root = doc.getDocumentElement();
parseElement(root);
}
private static void parseElement(Element element)
{
String tagName = element.getNodeName();
NodeList children = element.getChildNodes();
System.out.print("<" + tagName);
//element元素的所有属性所构成的NamedNodeMap对象,需要对其进行判断
NamedNodeMap map = element.getAttributes();
//如果该元素存在属性
if(null != map)
{
for(int i = 0; i < map.getLength(); i++)
{
//获得该元素的每一个属性
Attr attr = (Attr)map.item(i);
String attrName = attr.getName();
String attrValue = attr.getValue();
System.out.print(" " + attrName + "=\"" + attrValue + "\"");
}
}
System.out.print(">");
for(int i = 0; i < children.getLength(); i++)
{
Node node = children.item(i);
//获得结点的类型
short nodeType = node.getNodeType();
if(nodeType == Node.ELEMENT_NODE)
{
//是元素,继续递归
parseElement((Element)node);
}
else if(nodeType == Node.TEXT_NODE)
{
//递归出口
System.out.print(node.getNodeValue());
}
else if(nodeType == Node.COMMENT_NODE)
{
System.out.print("<!--");
Comment comment = (Comment)node;
//注释内容
String data = comment.getData();
System.out.print(data);
System.out.print("-->");
}
}
System.out.print("</" + tagName + ">");
}
}
public class SaxTest1
{
public static void main(String[] args) throws Exception
{
//step1: 获得SAX解析器工厂实例
SAXParserFactory factory = SAXParserFactory.newInstance();
//step2: 获得SAX解析器实例
SAXParser parser = factory.newSAXParser();
//step3: 开始进行解析
parser.parse(new File("student.xml"), new MyHandler());
}
}
class MyHandler extends DefaultHandler
{
@Override
public void startDocument() throws SAXException
{
System.out.println("parse began");
}
@Override
public void endDocument() throws SAXException
{
System.out.println("parse finished");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
System.out.println("start element");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
System.out.println("finish element");
}
}
public class JDomTest1
{
public static void main(String[] args) throws Exception
{
Document document = new Document();
Element root = new Element("root");
document.addContent(root);
Comment comment = new Comment("This is my comments");
root.addContent(comment);
Element e = new Element("hello");
e.setAttribute("sohu", "www.sohu.com");
root.addContent(e);
Element e2 = new Element("world");
Attribute attr = new Attribute("test", "hehe");
e2.setAttribute(attr);
e.addContent(e2);
e2.addContent(new Element("aaa").setAttribute("a", "b")
.setAttribute("x", "y").setAttribute("gg", "hh").setText("text content"));
Format format = Format.getPrettyFormat();
format.setIndent(" ");
// format.setEncoding("gbk");
XMLOutputter out = new XMLOutputter(format);
out.output(document, new FileWriter("jdom.xml"));
}
}
⑵ 使用java开发的表达式解析框架有哪些
class BinaryExpressionParser : ExpressionParser<BinaryExpression>
{
public override void Where(BinaryExpression expr, ParserArgs args)
{
if (ExistsBracket(expr.Left))
{
args.Builder.Append(' ');
args.Builder.Append('(');
Parser.Where(expr.Left, args);
args.Builder.Append(')');
}
else
⑶ JAVA正则表达式解析HTML字符串
public class TestString4 {
public static void main(String[] args) {
String s = "<R_Data> 0005,实验室0,0,0|0101,实验室A-测试点1,200,200|0102,实验室C-测试点2,80,400|0109,实验室C-测试点1,80,300|1020,实验室C-测试点3,80,500|1141,实验室A-测试点2,400,400|1146,实验室A-测试点3,300,300|1239,实验室B-测试点1,50,150|1240,实验室B-测试点2,80,200|1264,实验室B-测试点3,220,110| </R_Data>";
s = s.replace("<R_Data>", "").replace("</R_Data>", "").trim();
String ss[] = s.split("\|");
String[][] sss = new String[ss.length][];
for(int i=0;i<ss.length;i++){
sss[i] = ss[i].split(",");
}
}
}
sss中存放的就是你需要的数据
⑷ JAVA中如何解析字符串公式,并且利用公式进行计算
可以使用 commons-jexl3 jar包
示例:
public static void main(String[] args){
String expressionString = "1+2+3";
JexlEngine jexlEngine = new JexlBuilder().create();
JexlExpression jexlExpression = jexlEngine.createExpression(expressionString);
Object evaluate = jexlExpression.evaluate(null);
System.out.println(evaluate);
}
结果: 6
示例2:
来个复杂点的
public static void main(String[] args){
// String expressionString = "1+2+3";
String expressionString = "100*10-(200+300)";
JexlEngine jexlEngine = new JexlBuilder().create();
JexlExpression jexlExpression = jexlEngine.createExpression(expressionString);
Object evaluate = jexlExpression.evaluate(null);
System.out.println(evaluate);
}
结果: 500
⑸ java:如何把字符串解析成一个方法
publicclassMethodTest{
publicstaticvoidmain(String[]args){
try
{
MethodTestmt=newMethodTest();
mt.a("getString('hello',2)",mt);
}catch(Exceptione){
e.printStackTrace();
}
}
//使用这种方法的前提是知道参数类型参数个数
publicvoida(StringmethodStr,Objectobj)throwsNoSuchMethodException,SecurityException,IllegalAccessException,IllegalArgumentException,InvocationTargetException{
//拆解方法字符串,找出方法名
StringmethodName=methodStr.substring(0,methodStr.indexOf('('));
//找出参数
Object[]args=null;
StringparamStr=methodStr.substring(methodStr.indexOf('(')+1,methodStr.indexOf(')'));
if(!paramStr.isEmpty()){
String[]tmp=paramStr.split(",");
args=newObject[tmp.length];
args[0]=tmp[0];
args[1]=Integer.valueOf(tmp[1]);
}
Classc=obj.getClass();
Methodmethod=c.getMethod(methodName,String.class,int.class);
method.invoke(obj,args);
}
publicvoidgetString(Stringstr,inti){
System.out.println(str+"*********"+i);
}
}
使用反射写的,不过感觉封装的不是很完善,需要提前知道方法的参数个数,参数类型,才能这样调用。----------如果没有参数,直接写getMethod那边传参直接为null就可以
⑹ 如何用java解析CSV文件
思想:先获取csv文件的路径,通过BufferedReader类去读该路径中的文件,使用readLine方法进行逐行读取。
注意:使用readLine方法后会自动转到下一行。因此在判断是否为空后得先将读取到的内容赋值给一变量,在循环中使用该变量即可。
publicstaticvoidmain(String[]args)
{
Filecsv=newFile("C:\Users\chenxumin\Desktop\Result.csv");//CSV文件路径
BufferedReaderbr=null;
try
{
br=newBufferedReader(newFileReader(csv));
}catch(FileNotFoundExceptione)
{
e.printStackTrace();
}
Stringline="";
StringeveryLine="";
try{
List<String>allString=newArrayList<>();
while((line=br.readLine())!=null)//读取到的内容给line变量
{
everyLine=line;
System.out.println(everyLine);
allString.add(everyLine);
}
System.out.println("csv表格中所有行数:"+allString.size());
}catch(IOExceptione)
{
e.printStackTrace();
}
}