導航:首頁 > 編程語言 > javaargs解析

javaargs解析

發布時間:2024-04-07 22:52:17

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();
}

}
閱讀全文

與javaargs解析相關的資料

熱點內容
無印良品書包在哪個APP買 瀏覽:140
大數據建模服務模型 瀏覽:841
復活老照片是什麼app 瀏覽:943
進電商哪些數據分析軟體是免費的 瀏覽:61
oracle做資料庫的程序源碼 瀏覽:201
汽車復合中心編程用什麼軟體好 瀏覽:955
蘋果xr小組件如何添加app 瀏覽:413
mvc配置文件詳解 瀏覽:655
維盟密碼怎麼設置圖解 瀏覽:779
末日類單機游戲安卓 瀏覽:809
ps的修補工具在哪裡 瀏覽:670
linuxcmake卸載 瀏覽:682
蘭州養老保險認證用什麼app 瀏覽:540
單頁文件夾 瀏覽:618
在的地方網路不好怎麼辦 瀏覽:443
越獄工具時間表 瀏覽:477
模擬銀行查詢系統java 瀏覽:471
央行302號文件解讀 瀏覽:435
為什麼想過要放棄編程 瀏覽:823
匯編程序員可見是什麼意思 瀏覽:779

友情鏈接