Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Java XML Processing
Search
Brian Hsu
January 08, 2013
Programming
92
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Java XML Processing
Processing XML in Java with XOM library.
Brian Hsu
January 08, 2013
More Decks by Brian Hsu
See All by Brian Hsu
我如何停止憂慮並愛上 Non-MVC Web Framework @ OSDC.tw 2013
brianhsu
6
3.4k
數位典藏聯合目錄搜尋引擎模組
brianhsu
0
220
Java Unicode NCR 處理
brianhsu
1
450
如何在 Java App 中導入 Scala @ JavaTWO 2011
brianhsu
1
140
[LT] 自由軟體讓你五分鐘上新聞 @ OSDC.tw 2011
brianhsu
1
120
ScalaTest-連貓都會的單元測試與 BDD @ COSCUP 2010
brianhsu
0
310
Introduction to Scala @ TWJUG 2010/07
brianhsu
1
200
Programming Android Application in Scala @ OSDC.tw 2010
brianhsu
1
140
Other Decks in Programming
See All in Programming
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
2
1.1k
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
370
Simple Storage Service(S3) is not simple
iwatsukayura
0
320
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
1
200
JAWS-UG 東京支部が始める、JAWS-UG支部コラボ / JAWS-UG lunchtime LT Collaboration
y0hgi
0
300
[2026-10-14](C)Re:ゼロから始める異世界生活〜始まりの終わりと終わりの始まり〜
tosite
0
150
Railsバージョンを4倍にした話
optim
0
550
graphql-ruby を Spinel で速くしようとした話
mtsmfm
0
120
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
200
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
5.3k
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
150
Kotlinの15年とServer-Side Kotlin
n_takehata
1
160
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
260
How to Talk to Developers About Accessibility
jct
2
590
We Are The Robots
honzajavorek
0
400
Agile that works and the tools we love
rasmusluckow
331
22k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
640
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
640
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
22k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
890
Designing for Timeless Needs
cassininazir
1
530
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
Transcript
Java XML 處理 BrianHsu
[email protected]
聯合目錄的 XML • DACatalog 格式 • OAI 格式 • 藏品
ID 的轉換 – 每個藏品都有唯一的整數流水編號 (OID/objectID) – 轉成十六進位,前補零至八位數後兩個一組 • 1123021 = 0x1122cd => 0x001122cd • 00/11/22/cd.xml • http://catalog.digitalarchives.tw/item/00/11/22/cd.html
Java XML Library • XOM – 單一的 JAR 檔 –
API 較簡單 – 對 XML 格式的嚴格要求 • JDOM
XOM 讀取 XML 檔案 import java.io.File; import nu.xom.*; // Builder
==> 解析 / 修改 XML 用 Builder builder = new Builder(); Document document = builder.build(new File("test.xml")); // 讀 XML 檔案 Document document = builder.build("<root></root>", null); // 讀 XML 字串 Element root = document.getRootElement(); // XML 的根節點 // 取得子節點 root.getChildElements() root.getChildElements("AdminDesc") root.getFirstChildElements() root.getFirstChildElements("MetaDesc") // 取得屬性 root.getAttribute("name").getValue() // XPath Query root.query("/AdminDesc") // 只找下一層 root.query("//Title") // 找這一層以下的所有東西 // 看目前的節點下的所有東西 root.toXML()
XOM 寫入 XML 檔 import nu.xom; import java.io.FileOutputStream; import java.io.File;
FileOutputStream outputFile = new FileOutputStream(new File("bbb.txt")); nu.xom.Serializer xmlFormatter = new nu.xom.Serializer(outputFile); // 寫成 UTF-8 nu.xom.Serializer xmlFormatter = new nu.xom.Serializer(outputFile, "Big5"); xmlFormatter.setIndent(2); // 縮排 xmlFormatter.write(xmlDocument.getDocument());
修改 XML 檔案 • 讀入 XML 檔案後修改節點 – Element#appendChild –
Element#insertChild – Element#addAttribute – Element#removeChild – Element#replaceChild • 在讀入時直接修改 – NodeFactory – 參照 XMLModel 裡的 VCenterXMLMaker 類別和 XMLPreprocseeor.saveAsVCenterXMLFile()
複製檔案 • Apache CommonsIO – import org.apache.commons.io.FileUtils – static void
copyFile(File srcFile, File destFile) – static void copyDirectory(File srcDir, File destDir) – static Collection<File> listFiles(File directory, String[] extensions, boolean recursive)
XMLModel • 確認專案目錄下有以下檔案 – src/main/resources/ProjectCatalogPrefix.txt – src/main/resources/SubjectCatalogPrefix.txt • XMLPreprocessor –
Element loadXML(File file, boolean cleanup) – Element loadXML(String xml, boolean cleanup) – void saveAsBig5XMLFile(Element xmlDocument, File outputFile) • DACatalog – 可以吃 DACatalog/OAI 格式的 XML 並轉成 Java 物件
XMLModel import tw.digitalarchives.util.XMLPreprocessor; import tw.digitalarchives.model.DACatalog; import nu.xom.*; import java.io.File; Element
xmlData = XMLPreprocessor.loadXML(File("test.xml")); DACatalog daCatalog = new DACatalog(xmlData);