使用默认命名空间读取XML

时间:2016-10-12 14:44:19

标签: xml xpath axapta

考虑这项有效的工作:

static void XMLTest(Args _args)
{
    str xml = @'<?xml version="1.0" encoding="UTF-8"?>
       <tests xmlns="abc">
           <test>
               <testnumber><id>1</id></testnumber>
               <testname>bla bla</testname>
           </test>
       </tests>
   ';
   XMlDocument doc = XMLDocument::newXML(xml);
   XMLNodeList tests = doc.selectNodes('//tests/test');
   XMLNode node;
   for (node = tests.nextNode(); node; node = tests.nextNode())
   {
       info(node.selectSingleNode('testnumber/id').text());
       info(node.selectSingleNode('testname').text());
   }
}

输出&#34; 1&#34;和&#34; bla bla&#34;如预期的那样。

现在从:

更改xml的第二行
<tests>

为:

<tests xmlns="xyz">

现在它无法读取任何内容。

如何使用默认命名空间读取XML?

2 个答案:

答案 0 :(得分:4)

您可以通过XMLNamespaceManager的实例严格按照这样做

static void XMLTest(Args _args)
{
    str xml = @'<?xml version="1.0" encoding="UTF-8"?>
       <tests xmlns="abc">
           <test>
               <testnumber><id>1</id></testnumber>
               <testname>bla bla</testname>
           </test>
       </tests>
   ';
   XMlDocument doc = XMLDocument::newXML(xml);
   XMLNamespaceManager nsMgr = new XMLNamespaceManager(new XmlNameTable());
   XMLNodeList tests;
   XMLNode node;
   ;

   nsMgr.addNamespace('x', 'abc');
   tests = doc.selectNodes("//x:tests/x:test", nsMgr);

   for (node = tests.nextNode(); node; node = tests.nextNode())
   {
       info(node.selectSingleNode('x:testnumber/x:id', nsMgr).text());
       info(node.selectSingleNode('x:testname', nsMgr).text());
   }
}

如果确定不会发生命名冲突,您可能希望忽略命名空间并通过local-name()匹配节点,如此

static void XMLTest2(Args _args)
{
    str xml = @'<?xml version="1.0" encoding="UTF-8"?>
       <tests xmlns="abc">
           <test>
               <testnumber><id>1</id></testnumber>
               <testname>bla bla</testname>
           </test>
       </tests>
   ';
   XMlDocument doc = XMLDocument::newXML(xml);
   XMLNodeList tests = doc.selectNodes("//*[local-name()='tests']/*[local-name()='test']");
   XMLNode node;
   for (node = tests.nextNode(); node; node = tests.nextNode())
   {
       info(node.selectSingleNode("*[local-name()='testnumber']/*[local-name()='id']").text());
       info(node.selectSingleNode("*[local-name()='testname']").text());
   }
}

答案 1 :(得分:0)

另一种方法,空白根的xmlns属性:

static void XMLTest(Args _args)
{
    str xml = @'<?xml version="1.0" encoding="UTF-8"?>
       <tests xmlns="abc">
           <test>
               <testnumber><id>1</id></testnumber>
               <testname>bla bla</testname>
           </test>
       </tests>
   ';
   XMlDocument doc = XMLDocument::newXML(xml);
   XMLNodeList tests;
   XMLNode node;
   ;
   doc.root().attributes().getNamedItem('xmlns').value(''); // Go away xmlns
   doc = XMLDocument::newXML(doc.xml());                    // Now reread all again
   tests = doc.selectNodes("//tests/x:test", nsMgr);   
   for (node = tests.nextNode(); node; node = tests.nextNode())
   {
       info(node.selectSingleNode('testnumber/id').text());
       info(node.selectSingleNode('testname'.text());
   }
}

这需要使用doc = XMLDocument::newXML(doc.xml())重新解析XML。如果XML很大,请选择@ DAXaholic的第一个解决方案。