【问题标题】:EXTJS 4.2 escape colon (:) in XML Tag for XML ReaderXML 阅读器的 XML 标记中的 EXTJS 4.2 转义冒号 (:)
【发布时间】:2013-11-28 01:57:58
【问题描述】:

我正在尝试将 atom pub CMIS 1.0 绑定加载到 EXTJS 中,并且标签中的冒号正在阻止 XML 阅读器完成其工作。例如xml看起来像:

<atom:entry xmlns:atom="http://www.w3.org/2005/Atom" xmlns:cmis="http://docs.oasis-         open.org/ns/cmis/core/200908/" xmlns:cmisra="http://docs.oasis-open.org/ns/cmis/restatom/200908/" xmlns:app="http://www.w3.org/2007/app">
  <atom:author>
      <atom:name>system</atom:name>
   </atom:author>
   <atom:id>http://chemistry.apache.org/MTAz</atom:id>
   <atom:published>2013-11-28T00:01:22Z</atom:published>
   <atom:title>Folder1</atom:title>
   <app:edited>2013-11-28T00:01:22Z</app:edited>
   <atom:updated>2013-11-28T00:01:22Z</atom:updated>
   <cmisra:object xmlns:ns3="http://docs.oasis-open.org/ns/cmis/messaging/200908/">
      <cmis:properties>
         <cmis:propertyId queryName="cmis:allowedChildObjectTypeIds" displayName="Allowed Child Types" localName="cmis:allowedChildObjectTypeIds" propertyDefinitionId="cmis:allowedChildObjectTypeIds">
            <cmis:value>*</cmis:value>
         </cmis:propertyId>
         <cmis:propertyId queryName="cmis:objectTypeId" displayName="Type-Id" localName="cmis:objectTypeId" propertyDefinitionId="cmis:objectTypeId">
            <cmis:value>cmis:folder</cmis:value>
         </cmis:propertyId>
         <cmis:propertyString queryName="cmis:path" displayName="Path" localName="cmis:path" propertyDefinitionId="cmis:path">
            <cmis:value>/Root/396271/Folder1</cmis:value>
         </cmis:propertyString>
         <cmis:propertyString queryName="cmis:name" displayName="Name" localName="cmis:name" propertyDefinitionId="cmis:name">
            <cmis:value>Folder1</cmis:value>
         </cmis:propertyString>
..... etc.

例如,如果我考虑一个简单的例子

    <?xml version="1.0" encoding="UTF-8"?>
<users>
    <user attr="test ed">
        <id:number>1</id:number>
        <name>Ed Spencer</name>
        <email>ed@sencha.com</email>
    </user>
    <user attr="test abe">
        <id:number>2</id:number>
        <name>Abe Elias</name>
        <email>abe@sencha.com</email>
    </user>
</users>

我使用 EXTJS 代码

Ext.onReady(function () {
   Ext.define('User', {
        extend: 'Ext.data.Model',
        autoload: true,
        fields: [{  name: "id", mapping: 'id:number'},
                         {  name: "name", mapping: 'name'},
                         {  name: "email", mapping: 'email'},
                         {  name: "attr", mapping: '@attr'}]
    });

    var store = Ext.create('Ext.data.Store', {
        model: 'User',
        proxy: {
            type: 'ajax',
            url : 'users.xml',
            reader: {
                type: 'xml',
                record: 'user',
                root: 'users'
            }
        }
    }); 

    store.load();

});

我应该为 id 或备用代码的映射记下什么?

fields: [{  name: "id", mapping: 'id:number'} ????? escape the ':' some how?

如果可能,我会尽量避免编写大量自定义 XML 解析代码并依赖 EXTJS 阅读器,并且 CMIS 1.1 浏览器绑定不适用于我的 ECM。

【问题讨论】:

    标签: javascript xml extjs extjs4 cmis


    【解决方案1】:

    我发现解决方案在 EXTJS 4 中得到解决,但在我熟悉的早期版本 (3.4) 中没有解决:命名空间字符 |在 EXTJS 4 DomQuery 中引入

    因此,如果示例 XML 具有定义的命名空间,例如 xmlns:id="http://my.example.com/id

    那么之前修改过命名空间的XML就是:

    <?xml version="1.0" encoding="UTF-8"?>
    <users xmlns:id="http://my.example.com/id">
        <id:user attr="test ed">
            <id:id>1</id:id>
            <id:name>
                <id:first attr2="test ed 2">Ed</id:first>
                <id:last>Spencer</id:last>
            </id:name>
            <id:email>ed@sencha.com</id:email>
        </id:user>
        <id:user attr="test abe">
            <id:id>2</id:id>
            <id:name>
                <id:first attr2="test ed 2">Abe</id:first>
                <id:last>Elias</id:last>
            </id:name>
            <id:email>abe@sencha.com</id:email>
        </id:user>
    </users>
    

    这样下面的 EXTJS 代码就可以工作了fields: [{ name: "id", mapping: 'id|id'}

    这就是完整的解决方案

    <!DOCTYPE html>
    <html>
    <head>
    <link href="http://cdn.sencha.com/ext/gpl/4.2.1/resources/css/ext-all.css" rel="stylesheet" />
    <script src="http://cdn.sencha.com/ext/gpl/4.2.1/ext-all.js"></script>
    <script>
    function getResult()
    {
     document.getElementById("demo").innerHTML= 
             "<p>First Record Id is: " + test.data.items[0].data.id + "</p>" +
             "<p> First Name is: " + test.data.items[0].data.first + "</p>" +
             "<p> Last Name is: " + test.data.items[0].data.last + "</p>" +
             "<p> Email is: " + test.data.items[0].data.email + "</p>" +
             "<p> Attribute 1 is: " + test.data.items[0].data.attr + "</p>" +
             "<p> Attribute 2 is: " + test.data.items[0].data.attr2 + "</p>";
    }
    
    var test;
    
    Ext.onReady(function () {
       Ext.define('User', {
            extend: 'Ext.data.Model',
            autoload: true,
            fields: [{  name: "id", mapping: 'id|id'},
                             {  name: "first", mapping: 'id|name>id|first'},
                             {  name: "last", mapping: 'id|name>id|last'},
                             {  name: "email", mapping: 'id|email'},
                             {  name: "attr", mapping: '@attr'},
                             {  name: "attr2", mapping: "id|name>id|first[@attr2='test ed 2']"}]
        });
    
        var store = Ext.create('Ext.data.Store', {
            model: 'User',
            proxy: {
                type: 'ajax',
                url : 'users.xml',
                reader: {
                    type: 'xml',
                    record: 'id|user',
                    root: 'users'
                }
            }
        }); 
    
        store.load();
    
        test = store;
    });
    </script>
    </head>
    <body>
    
    <h1>EXTJS Example</h1>
    <p id="demo">Here we try to load XML into an EXTJS XML Reader</p>
    
    <button type="button" onclick="getResult();">getResult</button>
    
    </body>
    </html> 
    

    还要注意,如果在定义命名空间之前在根元素中使用了命名空间,那么 XML 阅读器中的根定义需要使用 : 而不是 |性格,即 root: 'id:users'

    【讨论】:

      【解决方案2】:

      我也面临同样的问题,我正在使用 extjs-4.2.1

      我的示例 xml

      <RDF>
          <Seq about="urn:productManagement:root" type="ROOT" ac:maxLevels="25">
              <li>
                  <Description ac:name="name1" ac:type="type1"/>
              </li>
          </Seq>
      </RDF>
      

      上述xml的模型

      Ext.define('TR.model.TreeControlModel', {
          extend: 'Ext.data.Model',
              fields: [
                  { name: 'about', mapping:'@about'},
                  { name: 'type', mapping:'li>Description@ac:type'},
                 { name: "name", mapping:'li>Description@ac:name'}
              ]
      });
      

      我尝试更换管道:用 |但这对我不起作用。需要帮助。

      【讨论】:

      • 您需要在示例的第一个 标记中添加 xmlns:ac="my.example.com/ac" 以便定义命名空间。然后 |会工作的。
      猜你喜欢
      • 1970-01-01
      • 2018-12-17
      • 2014-05-18
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多