2013年3月24日 星期日

java replaceAll() - 關於反斜線的小技巧


如果要用 java replaceAll() 這函數替換反斜線
一個反斜線 "\" 等於 "\\\\"四個反斜線,
例如
要將str1和str2的反斜線取代

str1="aa\bbb"; str2="aa'bbb";
str1="aa\\bbb";str2="aa\'bbb";

要這樣寫
str1 = str1.replaceAll("\\\\", "\\\\\\\\");
str2 = str2.replaceAll("'", "\\\\'");

為什麼呢? 因為JAVA將"\\\\"解析成"\\"給正規表達式,正規表達式再將"\\"解析成"\"
所以一個反斜線,在正規表達式要寫成四個

2013年3月22日 星期五

Eclipse 無法複製貼上

多謝這篇文章救了我   http://otweb.com/phramework/pw/module/blog/index.php?id=935

Window > Preferences > Java > Editor > Typing
取消勾選  Update imports

這樣做之後就能正常複製貼上囉

JAVA parse XML 範例 (DOM parser)

根據這篇文章,可以知道JAVA有4種解析XML的方法


  1)DOM(JAXP Crimson解析器)
  2)SAX
  3)JDOM http://www.jdom.org
  4)DOM4J http://dom4j.sourceforge.net


在不求效能的情況下,這裡以最簡單的DOM parser作範例,參考這篇文章


staff.xml
<?xml version="1.0"?>
<company>
 <staff id="1001">
  <firstname>yong</firstname>
  <lastname>mook kim</lastname>
  <nickname>mkyong</nickname>
  <salary>100000</salary>
 </staff>
 <staff id="2001">
  <firstname>low</firstname>
  <lastname>yin fong</lastname>
  <nickname>fong fong</nickname>
  <salary>200000</salary>
 </staff>
</company>
程式碼


ReadXMLFile.java
package com.mkyong.seo;
 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
 
public class ReadXMLFile {
 
  public static void main(String argv[]) {
 
    try {
 
 File fXmlFile = new File("/Users/mkyong/staff.xml");
 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
 Document doc = dBuilder.parse(fXmlFile);
 
 //optional, but recommended
 //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
 doc.getDocumentElement().normalize();
 
 System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
 
 NodeList nList = doc.getElementsByTagName("staff");
 
 System.out.println("----------------------------");
 
 for (int temp = 0; temp < nList.getLength(); temp++) {
 
  Node nNode = nList.item(temp);
 
  System.out.println("\nCurrent Element :" + nNode.getNodeName());
 
  if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
   Element eElement = (Element) nNode;
 
   System.out.println("Staff id : " + eElement.getAttribute("id"));
   System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
   System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
   System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
   System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
 
  }
 }
    } catch (Exception e) {
 e.printStackTrace();
    }
  }
 
}


結果


Root element :company
----------------------------
 
Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
 
Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000