遍历XML文档的所有节点?

发布于 2024-12-08 04:18:02 字数 588 浏览 0 评论 0原文

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

请找到示例文档。

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

在本文档中,我需要遍历节点、<标题>、<短标题>、<粗体> 等。

提前致以

问候, 亚历克斯

I want to traverse through the entire nodes of an 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.

Thanks in advance

Regards,
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

地狱即天堂 2024-12-15 04:18:02

第一步是按如下方式解析 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( ).

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

这对于相当小的 XML 文档来说是可以的,但为了提高效率,如果您是寻找一组特定的节点,您可能需要考虑使用 XPATH 查询。

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:

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.

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.

若沐 2024-12-15 04:18:02

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

“iXML ABAP 对象快速入门”一章应该可以帮助您快速入门。 “迭代整个 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.

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').

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

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.
沫离伤花 2024-12-15 04:18:02

我希望下面的例子可以澄清这种情况:

 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.

享受,
亚历山大.

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.

小瓶盖 2024-12-15 04:18:02

在不断变化的环境中,手动 xml 遍历容易出错且复杂。您可能想检查是否真的需要直接代码遍历。

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

转换的声明、测试和调试是使用事务 STRANS 打开的转换编辑器完成的。

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

在您的 ABAP 代码中,您只需调用语言元素 CALL TRANSFORMATION 然后数据就准备好在目标结构中进行处理:
ABAP 语句:“调用转换”

Manual xml traversing is error prone and complicated in changing environments. 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. XPath is supported.

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

XSLT is available as transformation type:
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'

溺ぐ爱和你が 2024-12-15 04:18:02

您可以使用 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
}

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文