250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- Source
- Spring Boot
- 설정
- IntelliJ
- Python
- spring
- error
- MySQL
- ubuntu
- STS
- 오픈소스
- PostgreSQL
- Thymeleaf
- git
- myBatis
- Eclipse
- JavaScript
- SpringBoot
- Exception
- JDBC
- Docker
- Core Java
- oracle
- maven
- 문서
- jpa
- Tomcat
- MSSQL
- Open Source
- AJAX
Archives
- Today
- Total
헤르메스 LIFE
[Source] DOM4j Modify XMLDemo 본문
728x90
출처 : http://www.tutorialspoint.com/java_xml/java_dom4j_modify_document.htm
package com.file .utils;
import java. io.File ;
import java. io.IOException ;
import java. io.UnsupportedEncodingException ;
import java. util.Iterator ;
import java. util.List ;
import org. dom4j.Document ;
import org. dom4j.DocumentException ;
import org. dom4j.Element ;
import org. dom4j.Node ;
import org. dom4j.io .OutputFormat;
import org. dom4j.io .SAXReader;
import org. dom4j.io .XMLWriter;
public class DOM4jModifyXMLDemo {
public static void main( String[] args ) {
try {
File inputFile = new File("C:/dev/workspace/JavaDev/src/com/file/utils/xPathTest.xml" );
SAXReader reader = new SAXReader ();
Document document = reader.read (inputFile) ;
Element classElement = document.getRootElement ();
List nodes = document .selectNodes("/class/student[@rollno='493']" );
for ( Node node : nodes) {
Element element = (Element) node;
Iterator iterator = element. elementIterator("marks") ;
while ( iterator.hasNext ()) {
Element marksElement = (Element) iterator.next ();
marksElement.setText ("80") ;
}
}
// Pretty print the document to System.out
OutputFormat format = OutputFormat.createPrettyPrint() ;
XMLWriter writer ;
writer = new XMLWriter(System. out, format);
writer.write (document) ;
} catch (DocumentException e) {
e.printStackTrace ();
} catch (UnsupportedEncodingException e) {
e.printStackTrace ();
} catch (IOException e) {
e.printStackTrace ();
}
}
}
결과
<?xml version="1.0" encoding="UTF-8"?>
<class> <student rollno="393"> <firstname>dinkar</firstname> <lastname>kad</lastname> <nickname>dinkar</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Vaneet</firstname> <lastname>Gupta</lastname> <nickname>vinni</nickname> <marks>80</marks> </student> <student rollno="593"> <firstname>jasvir</firstname> <lastname>singh</lastname> <nickname>jazz</nickname> <marks>90</marks> </student> </class>
|
728x90
'Core Java' 카테고리의 다른 글
[Source] XML 파싱 (0) | 2020.12.20 |
---|---|
[Source] Create Xml File Demo (0) | 2020.12.20 |
[Source] XPatharserDemo (0) | 2020.12.20 |
[HttpURLConnection] jsp 파일의 실행 (0) | 2020.12.17 |
외부프로그램 실행시키기 (0) | 2020.12.17 |