Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Java XML Processing
Search
Brian Hsu
January 08, 2013
Programming
0
71
Java XML Processing
Processing XML in Java with XOM library.
Brian Hsu
January 08, 2013
Tweet
Share
More Decks by Brian Hsu
See All by Brian Hsu
我如何停止憂慮並愛上 Non-MVC Web Framework @ OSDC.tw 2013
brianhsu
6
3.3k
數位典藏聯合目錄搜尋引擎模組
brianhsu
0
140
Java Unicode NCR 處理
brianhsu
1
350
如何在 Java App 中導入 Scala @ JavaTWO 2011
brianhsu
1
88
[LT] 自由軟體讓你五分鐘上新聞 @ OSDC.tw 2011
brianhsu
1
81
ScalaTest-連貓都會的單元測試與 BDD @ COSCUP 2010
brianhsu
0
200
Introduction to Scala @ TWJUG 2010/07
brianhsu
1
160
Programming Android Application in Scala @ OSDC.tw 2010
brianhsu
1
91
Other Decks in Programming
See All in Programming
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
160
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
170
リアーキテクチャxDDD 1年間の取り組みと進化
hsawaji
1
200
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
2.5k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.3k
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
210
CSC509 Lecture 12
javiergs
PRO
0
140
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
340
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.7k
EventSourcingの理想と現実
wenas
6
2.2k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
3
970
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
0
190
Featured
See All Featured
The World Runs on Bad Software
bkeepers
PRO
65
11k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Ruby is Unlike a Banana
tanoku
96
11k
The Cult of Friendly URLs
andyhume
78
6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.8k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
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);