简体   繁体   English

遍历一个 XML 文档的所有节点?

[英]Traverse all the nodes of an XML document?

I want to traverse through the entire nodes of an if_ixml_document.我想遍历 if_ixml_document 的整个节点。 which is the best way to do this?这是最好的方法吗?

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.在本文档中,我需要遍历节点<text>, <id>, <guid> , <title>, <short_title>, <bold>等。

Thanks in advance提前致谢

Regards, Alex问候,亚历克斯

The first step is to parse your XML as follows.第一步是按如下方式解析您的 XML。 You can of course upload the XML from a file into the string, but this is just an example:您当然可以将文件中的 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.现在您有一个指向 XML 文档根的 IF_XML_NODE 实例。 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 树,并使用各种方法(例如 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 查询。

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. “iXML ABAP 对象快速入门”一章应该能让您快速入门。 The paragraph 'Iterating over the complete DOM-tree' provides the following example code: “迭代完整的 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.

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.享受吧,亚历山大。

Manual xml traversing is error prone and complicated in changing environments.手动 xml 遍历在不断变化的环境中容易出错且复杂。 You may want to check whether you really need a direct code traversal.您可能想要检查是否真的需要直接代码遍历。

With the help of (XSLT) transformations you are able to convert XML into ABAP structured types.在 (XSLT) 转换的帮助下,您可以将 XML 转换为 ABAP 结构化类型。 XPath is supported.支持 XPath。

Declaration, test and debugging of transformations is done using the Transformation Editor opened by transaction STRANS .转换的声明、测试和调试是使用由事务STRANS打开的转换编辑器完成的。

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'

You can use the DocumentTraversal interface which should be implemented by any DOM library (Xerces has it):您可以使用应该由任何 DOM 库实现的DocumentTraversal接口(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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM