HTML is the most common markup language for web development. HTML is a superset of XML, which is to say it is an extension of the XML specification. What is cool about this fact is that web browsers, in their ability to render HTML, actually come with XML parsers, and have XML parsing capabilities under the hood.
Why Think About XML At All
HTML is the ubiquitous markup language of internet developers. The audience of this blog, software engineers, likely only has need for HTML. Yet, my Media Company deals with many authors of the non-technical variety, and I have got to say... Authors think about their content wayyy differently than HTML gives credit for.
The beauty of XML is its generic stucture which allows for custom parsing and handling. This flexibiliy has been beautifully exemplified in HTML, but the use case of allowing custom definitions is better handled by XML.
XML is a data-carrying language. HTML is an extension of that language that comes with standardized graphical-user interface rendering. To see what I mean by this, open an XML file in a browser. https://alexason.com/uploads/library.xml
As you will see, modern browsers render the file complete with element tags. But also take note that the browser recognizes the datatype, and applies special formatting. In this way, XML is more like JSON.
Parsing XML
While not native to browser rendering agents, it's possible to parse XML using the browser API's DOMParser.
See a gist of this is action
const xmlString = `
<story>
<styles>
<titlestyle>
<color>#4A90E2</color>
</titlestyle>
<paragraphstyle>
<color>#333333</color>
</paragraphstyle>
</styles>
<title>Elena and the Embrace of Holiness</title>
<paragraph>In the heart of the village, where the sun kissed the earth...</paragraph>
<!-- More paragraphs here -->
</story>`;
const parser = new DOMParser();
const xmlDocument = parser.parseFromString(xmlString, "text/xml");
const parserError = xmlDoc.getElementsByTagName("parsererror");
if (parserError.length > 0) {
// Handle error
console.error("Error parsing XML:", parserError[0].textContent);
} else {
// Successfully parsed the XML
// XML Document contains a document
console.log("Parsed XML Document:", xmlDocument);
const title = xmlDocument.getElementsByTagName("title")[0].textContent;
const titleColor = xmlDocument.getElementsByTagName("color")[0].textContent;
}
Real Use Case
The example shown demonstrates what is possible with XML, yet the use case of rendering and styling content is better handled by HTML. While the format, resembles HTML, using XML as HTML must not be the best case of XML.
My HTML Developer I know, Israel, writes XML like this. He uses the data format to recreate HTML, then uses JavaScript to make it HTML. While this is possible given the flexibility of XML, if the only use case is for the browser, I'll tell you what I tell Israel: "Just write HTML!"
Join Israel and the HTML Devs at Salvation.
Where to use XML
XML is a great format for intermediate representation. As mentioned, the immediate use case of my company is translating many different Author's (book authors, manuscript writers) representation of their work into a standardized format. The task is to turn Word documents, PDFs, plaintext, and spoken words into some similar data format.
XML could do that, and is exactly used as such in software programs such as Calibre and Manuskript.
This has been a look at XML. It is a widely-recognized format, compatible with many readers and conversion tools. Given it's ease of parsing, W3C recommendation, and ubiquity, XML is a safe language for indefinite data storage.
If you're interested in tools for data science and storage, be sure to Follow this Dev.to. Add a reaction ? for more content like this.
A
以上是解析XML的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript字符串替換方法詳解及常見問題解答 本文將探討兩種在JavaScript中替換字符串字符的方法:在JavaScript代碼內部替換和在網頁HTML內部替換。 在JavaScript代碼內部替換字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 該方法僅替換第一個匹配項。要替換所有匹配項,需使用正則表達式並添加全局標誌g: str = str.replace(/fi

因此,在這裡,您準備好了解所有稱為Ajax的東西。但是,到底是什麼? AJAX一詞是指用於創建動態,交互式Web內容的一系列寬鬆的技術。 Ajax一詞,最初由Jesse J創造

10款趣味橫生的jQuery遊戲插件,讓您的網站更具吸引力,提升用戶粘性!雖然Flash仍然是開發休閒網頁遊戲的最佳軟件,但jQuery也能創造出令人驚喜的效果,雖然無法與純動作Flash遊戲媲美,但在某些情況下,您也能在瀏覽器中獲得意想不到的樂趣。 jQuery井字棋遊戲 遊戲編程的“Hello world”,現在有了jQuery版本。 源碼 jQuery瘋狂填詞遊戲 這是一個填空遊戲,由於不知道單詞的上下文,可能會產生一些古怪的結果。 源碼 jQuery掃雷遊戲

本教程演示瞭如何使用jQuery創建迷人的視差背景效果。 我們將構建一個帶有分層圖像的標題橫幅,從而創造出令人驚嘆的視覺深度。 更新的插件可與JQuery 1.6.4及更高版本一起使用。 下載

本文討論了在瀏覽器中優化JavaScript性能的策略,重點是減少執行時間並最大程度地減少對頁面負載速度的影響。

本文演示瞭如何使用jQuery和ajax自動每5秒自動刷新DIV的內容。 該示例從RSS提要中獲取並顯示了最新的博客文章以及最後的刷新時間戳。 加載圖像是選擇

Matter.js是一個用JavaScript編寫的2D剛體物理引擎。此庫可以幫助您輕鬆地在瀏覽器中模擬2D物理。它提供了許多功能,例如創建剛體並為其分配質量、面積或密度等物理屬性的能力。您還可以模擬不同類型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流瀏覽器。此外,它也適用於移動設備,因為它可以檢測觸摸並具有響應能力。所有這些功能都使其值得您投入時間學習如何使用該引擎,因為這樣您就可以輕鬆創建基於物理的2D遊戲或模擬。在本教程中,我將介紹此庫的基礎知識,包括其安裝和用法,並提供一


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1
強大的PHP整合開發環境