[翻译]  Abap - Traversing the entire nodes of an xml document?

[CHINESE]  Abap - 遍历xml文档的整个节点?


I want to traverse through the entire nodes of an if_ixml_document. which is the best way to do this?

我想遍历if_ixml_document的整个节点。这是最好的方法吗?

Please find the sample document.

请查找示例文档。

<text>
    <id>
         <guid auto="false">
               432543254543
         </guid>
    </id>
     <title>
         <short_title italics="on">
                <bold language = "german">
                     "Hello"
               </bold>
        </short_title>
     </title> </text>

In this document, i need to traverse through the nodes <text>, <id>, <guid> , <title>, <short_title>, <bold> etc.

在本文档中,我需要遍历节点 等。

Thanks in advance

提前致谢

Regards, Alex

问候,亚历克斯

5 个解决方案

#1


2  

You can find an extensive XML manual on SAP's documentation website (in case the link doesn't work correctly, go to the NetWeaver Developer's Guide on help.sap.com and search for 'xml library').

您可以在SAP的文档网站上找到大量的XML手册(如果链接无法正常工作,请访问help.sap.com上的NetWeaver开发人员指南并搜索“xml库”)。

The chapter 'iXML ABAP Objects Jumpstart' should get you started quickly. The paragraph 'Iterating over the complete DOM-tree' provides the following example code:

“iXML ABAP Objects Jumpstart”一章可以让您快速入门。 “迭代完整的DOM树”段落提供了以下示例代码:

data: iterator type ref to if_ixml_node_iterator,
      node     type ref to if_ixml_node.
iterator = document->create_iterator( ).
node = iterator->get_next( ).
while not node is initial.
  * do something with the node
  ...
  node = iterator->get_next( ).
endwhile.

#2


4  

The first step is to parse your XML as follows. You can of course upload the XML from a file into the string, but this is just an example:

第一步是按如下方式解析XML。您当然可以将XML从文件上传到字符串中,但这只是一个示例:

data: lr_xml type ref to cl_xml_document.
data: lr_node type ref to if_ixml_node.
data: lv_xml type string.

lv_xml = '<text> <id> <guid auto="false"> 432543254543 </guid> </id> <title> <short_title italics="on"> <bold language = "german"> "Hello"</bold> </short_title> </title> </text>'.

create object lr_xml.

lr_xml->parse_string( lv_xml ).
lr_node = lr_xml->get_first_node( ).

Now you have an instance of IF_XML_NODE that points to the root of your XML document. You can now use the various methods to traverse the XML tree, and get values out of it, using the various methods such as GET_CHILDREN, GET_ATTRIBUTES, GET_NAME etc.

现在,您有一个指向XML文档根目录的IF_XML_NODE实例。您现在可以使用各种方法遍历XML树,并使用各种方法(例如GET_CHILDREN,GET_ATTRIBUTES,GET_NAME等)从中获取值。

This will be OK for fairly small XML documents, though for efficiency, if you are looking for a specific set of nodes, you may want to look at using an XPATH query.

这对于相当小的XML文档是可以的,但是为了提高效率,如果要查找特定的节点集,可能需要查看使用XPATH查询。

#3


1  

I hope following example can clarify the situation:

我希望以下示例可以澄清情况:

 DATA: lcl_xml_doc TYPE REF TO cl_xml_document,
          lf_node TYPE REF TO if_ixml_node,
          lf_value TYPE string,
          i_xml type string,
          lf_name TYPE string,
      i_xml = 'PUT your XML HERE'.
      CREATE OBJECT lcl_xml_doc.
      IF lcl_xml_doc IS BOUND.
        IF lcl_xml_doc->parse_string( i_xml ) EQ 0.
          lf_node = lcl_xml_doc->m_document.
          IF  lf_node IS NOT INITIAL. 
            lf_iterator = lf_node->create_iterator( ).
            lf_node = lf_iterator->get_next( ).
            WHILE NOT lf_node IS INITIAL.
              lf_name = lf_node->get_name( ).
              lf_value = lf_node->get_value( ).
              IF lf_name = 'text'.
               " do something for text
               ENDIF.
              ENDIF.
              lf_node = lf_iterator->get_next( ).
            ENDWHILE.
        ENDIF.

Enjoy, Alexander.

享受,亚历山大。

#4


0  

Manual xml traversing is error prone and complicated in changing environments. You may want to check whether you really need a direct code traversal.

手动xml遍历在更改环境中容易出错且复杂。您可能想要检查是否确实需要直接代码遍历。

With the help of (XSLT) transformations you are able to convert XML into ABAP structured types. XPath is supported.

借助(XSLT)转换,您可以将XML转换为ABAP结构化类型。支持XPath。

Declaration, test and debugging of transformations is done using the Transformation Editor opened by transaction STRANS.

使用事务STRANS打开的Transformation Editor完成转换的声明,测试和调试。

XSLT is available as transformation type: ABAP XSLT Transformation

XSLT可用作转换类型:ABAP XSLT Transformation

In your ABAP Code you will just call the language element CALL TRANSFORMATION and the data is ready to process in your target structure afterwards: ABAP Statement: 'CALL TRANSFORMATION'

在您的ABAP代码中,您只需调用语言元素CALL TRANSFORMATION,然后数据就可以在目标结构中处理:ABAP语句:'CALL TRANSFORMATION'

#5


-1  

You can use the DocumentTraversal interface which should be implemented by any DOM library (Xerces has it):

您可以使用DocumentTraversal接口,该接口应由任何DOM库实现(Xerces具有它):

Document doc = ...;
NodeIterator i = ((DocumentTraversal) doc).createNodeIterator(doc, 
       NodeFilter.SHOW_ELEMENT, null, false);
Element e = null;
while ((e = (Element) i.nextNode()) != null) {
    // do stuff with element
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号