일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- rowspan
- sencha touch
- PHP
- appspresso
- jQuery
- node.js
- 가우스
- MFC
- GPS
- phonegap
- Google Map
- Spring
- 선택적조인
- 전자정부프레임워크
- ibsheet
- tomcat
- Eclipse
- jsr 296
- Android
- dock
- oracle
- JDOM
- Struts
- Ajax
- swingx
- MySQL
- WebLogic
- PLSQL
- iBATIS
- JSON
Archives
- Today
- Total
Where The Streets Have No Name
JDom - 기존 xml파일에 내용 추가 본문
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class CdrXmlDemo4 {
public static void main(String[] args) {
try {
SAXBuilder b = new SAXBuilder(); // true -> validate
// Create a JDOM document.
Document doc = b.build(new File("msc.xml"));
XPath xpath = XPath.newInstance("//cdr/message/message-id");
List list = xpath.selectNodes(doc);
int cnt = 1;
for (Iterator iter = list.iterator(); iter.hasNext();) {
Element elem = (Element) iter.next();
System.out.println(elem.getName() + " ," + elem.getText());
if(elem.getTextTrim().equals("123456")){
XPath xpath2 = XPath.newInstance("//cdr/message["+cnt+"]/msc/pdu-data");
Element pduData = (Element)xpath2.selectSingleNode(doc);
Element pduKey = new Element("pdu-key");
pduKey.setText("pdu key");
pduData.addContent(pduKey);
Element pduValue = new Element("pdu-value");
pduKey.setText("pdu key");
pduData.addContent(pduValue);
pduKey.setText("pdu value");
}
cnt++;
}
// Create an output formatter, and have it write the doc.
FileOutputStream out = new FileOutputStream("msc.xml");
XMLOutputter serializer = new XMLOutputter();
Format f = serializer.getFormat();
f.setEncoding("UTF-8");
f.setIndent(" ");
f.setLineSeparator("\r\n");
f.setTextMode(Format.TextMode.TRIM);
serializer.setFormat(f);
serializer.output(doc, out);
out.flush();
out.close();
} catch (JDOMException jex) {
System.out.print("PARSE ERROR: " + jex.getMessage());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
jdom에서 xpath를 사용하기 위해서는 jaxen을 같이 설치해야함