COBOL XML PARSE 不提供位置信息 (AFAIK)。确定给定事件发生的偏移位置是不可能的。对感兴趣的标签进行文本搜索必然会导致大量问题 - 甚至不要考虑去那里......
我能想到的下一个可能性是在解析原始文档期间构造感兴趣的 XML 片段文档。片段完成后,将其写入您的 XML 数据库。
下面的示例程序说明了您可以如何去做。该程序采用 XML-SOURCE 文档并对其进行解析以查找感兴趣的事件(例如,“b”标签)。当找到这样的标签时,它开始构建一个片段文档(XML-FRAGMENT),直到找到结束标签。此时片段被输出并开始搜索下一个片段。
IDENTIFICATION DIVISION.
PROGRAM-ID. XMLTEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
77 XL PIC S9(4) BINARY.
01. PIC X.
88 XML-EXTRACT-NO VALUE 'N'.
88 XML-EXTRACT-YES VALUE 'Y'.
01.
05 XML-FRAGMENT PIC X(8000).
05 XML-CHARS PIC S9(4) BINARY.
01 XML-SOURCE PIC X(8000).
PROCEDURE DIVISION.
MAINLINE SECTION.
MOVE '<a><b><c>Content</c><c>More content</c></b></a>'
TO XML-SOURCE
SET XML-EXTRACT-NO TO TRUE
XML PARSE XML-SOURCE
PROCESSING PROCEDURE XTRACT
GOBACK
.
XTRACT SECTION.
COMPUTE XL = FUNCTION LENGTH (XML-TEXT)
EVALUATE XML-EVENT
WHEN 'START-OF-ELEMENT'
*
* New XML element: Ignore it, add to existing fragment
* or start a new fragment...
*
IF XML-TEXT(1:XL) = 'b' OR XML-EXTRACT-YES
IF XML-EXTRACT-NO
MOVE ZERO TO XML-CHARS
SET XML-EXTRACT-YES TO TRUE
END-IF
STRING '<' XML-TEXT(1:XL) '>' DELIMITED BY SIZE
INTO XML-FRAGMENT(XML-CHARS + 1 : XL + 2)
COMPUTE XML-CHARS = XML-CHARS + XL + 2
END-IF
WHEN 'END-OF-ELEMENT'
*
* End of event: Add to XML fragment, ignore fragment or
* output complete fragment and start search
* for a new one...
*
IF XML-EXTRACT-YES
STRING '</' XML-TEXT(1:XL) '>' DELIMITED BY SIZE
INTO XML-FRAGMENT(XML-CHARS + 1 : XL + 3)
COMPUTE XML-CHARS = XML-CHARS + XL + 3
END-IF
IF XML-TEXT(1:XL) = 'b'
*
* End of fragment write to database (or whatever)
*
DISPLAY XML-FRAGMENT(1:XML-CHARS)
SET XML-EXTRACT-NO TO TRUE
END-IF
WHEN 'CONTENT-CHARACTERS'
*
* Add event data to fragment or just ignore it...
*
* Depening on the structure of your XML docuemnt you may
* need to react to addtional EVENTS such as CDATA to ensure
* proper tag construction.
*
IF XML-EXTRACT-YES
MOVE XML-TEXT(1:XL) TO XML-FRAGMENT(XML-CHARS + 1 : XL)
COMPUTE XML-CHARS = XML-CHARS + XL
END-IF
END-EVALUATE
.
此程序示例的输出为:
<b><c>Content</c><c>More content</c></b>
这不是您可能一直在寻找的单线解决方案,但您必须记住这是 COBOL!