Сообщение от leon2009sp
|
Здравствуйте! можно простой примерчик чтение файла xml;
Код:
|
<?xml version="1.0" encoding="utf-8"?>
<category>
<book name="kat1">
<page name="111.php">data1</page>
<page name="222.php">data2</page>
<page name="333.php">data3</page>
<page name="444.php">data4</page>
</book>
<book name="kat2">
<page name="111.php">data5</page>
<page name="222.php">data6</page>
<page name="333.php">data7</page>
<page name="444.php">data8</page>
</book>
</category> |
|
xPath, XSLT ну или просто навигация по DOM.
<?xml version="1.0" encoding="utf-8"?>
<category>
<book name="kat1">
<page name="111.php">data1</page>
<page name="222.php">data2</page>
<page name="333.php">data3</page>
<page name="444.php">data4</page>
</book>
<book name="kat2">
<page name="111.php">data5</page>
<page name="222.php">data6</page>
<page name="333.php">data7</page>
<page name="444.php">data8</page>
</book>
</category>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="category/book">
<ul>
<xsl:value-of select="@name"/>
<xsl:for-each select="page">
<li>
<xsl:value-of select="." />
</li>
</xsl:for-each>
</ul>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Документ без названия</title>
<style>
li{
padding-left : 20px;
list-style-type : none;
}
</style>
<script>
function loadXMLDoc(filename) {
if (window.ActiveXObject) {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
try { xhttp.responseType = "msxml-document" } catch (err) { } // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}
function displayResult() {
xml = loadXMLDoc("testXML.xml");
xsl = loadXMLDoc("testXSL.xsl");
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document") {
ex = xml.transformNode(xsl);
document.getElementById("example").innerHTML = ex;
}
// code for Chrome, Firefox, Opera
else if (document.implementation && document.implementation.createDocument) {
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml, document);
document.getElementById("example").appendChild(resultDocument);
}
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>