YUI:如何从本地文件中检索XML数据?

时间:2013-11-18 14:37:51

标签: javascript xml yui

我想读取存储在与此HTML文件相同的目录中的XML文件:

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <script src="js/libs/yui/yui-min.js"></script>
    <script type="text/javascript">
        function lancement() {
            alert("début fonction");
            var myDS;
            YUI().use("datasource-io", "datatable-base", "datasource-xmlschema", "datatabledatasource", "io", "datasource-textschema", "datasource-get", function(Y) {
                myDS = new YAHOO.util.LocalDataSource('test.xml', {
                    responseType: YAHOO.util.DataSource.TYPE_XML,
                    responseSchema: {
                        resultNode: "Customer",
                        totalRecords: "TotalCount",
                        fields: ["Name", "Id"]
                    }
                });
            });
            alert("fin fonction");
            alert(myDS.size());
        }
    </script>
</head>
<body class="yui3-skin-sam" onload="lancement();">
    <p>HELLO</p>
    <div id="tab1"></div>


</body>
</html>

这是XML文件:

<?xml version="1.0"?> 
<Customers>
    <TotalCount>1</TotalCount>
    <Customer><Name>John</Name><Id>1</Id></Customer>
</Customers>

但我永远无法读取行数。 你知道这个错误来自哪里吗?

感谢


好的,我阅读了教程的一部分,这是结果,它允许我显示一个表,但遗憾的是没有数据(XML文件是相同的):

<!DOCTYPE html>
<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width">
    <!--<script src="js/libs/yui/yui-min.js"></script>-->
    <script src="http://yui.yahooapis.com/3.13.0/build/yui/yui-min.js"></script>


    <script >
        YUI().use("datasource-io", "datatable-base", "datasource-xmlschema", "io", "datasource-textschema", "datasource-get", "datasource", "datatable", "datatable-datasource", function(Y) {
            myDS2 = new Y.DataSource.IO({source: "test.xml"});
            myDS2.responseType = Y.DataSource.Type_XML;
//    myDS2.responseSchema = {
//        resultNode: "Customer",
//        totalRecords: "TotalCount",
//        fields: ["Name", "Id"]
//    };
            myDS2.plug(Y.Plugin.DataSourceXMLSchema, {
                schema: {
                    resultListLocator: "result",
                    resultFields: [
                        {key: "Name", locator: "*[local-name() ='Name']"},
                        {key: "Id", locator: "*[local-name() ='Id']"}

                    ]
                }
            });
            var myColumnDefs = [
                {key: "Name", sortable: true, resizeable: true},
                {key: "Id", sortable: true, resizeable: true}];
            var myDataTable = new Y.DataTable({
                columns: myColumnDefs, data: myDS2});
            myDataTable.render("#tab1");
        });

    </script>
</head>
<body class="yui3-skin-sam">
    <p>HELLO</p>
    <div id="tab1"></div>


</body>
</html>
你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

在DataSourceXMLSchema中尝试:

 myDS2.plug(Y.Plugin.DataSourceXMLSchema, {
            schema: {
                resultListLocator: "Customers/Customer",
                resultFields: [
                    {key: "Name", locator: "Name"},
                    {key: "Id", locator: "Id"}

                ]
            }