Highlighted
Absent Member.
Absent Member.
2749 views

[archive] Trying to Get the Parent from an XML file

[Migrated content. Thread originally posted on 07 September 2007]

Below is the XML file that I am parsing. I am performing the following code, but it will not give me the Parent


000000 50001-GET-XML-FILE.
000000 CALL "C$XML" USING CXML-PARSE-FILE, PASSED-FILE-ALL.
000000 MOVE RETURN-CODE TO PARSER-HANDLE.
RRRRRR*Get Parent Name
RRRRRR CALL "C$XML" USING CXML-GET-PARENT, PARSER-HANDLE.
RRRRRR MOVE RETURN-CODE TO ELE-1-HANDLE.
RRRRRR CALL "C$XML" USING CXML-GET-DATA,
RRRRRR ELE-1-HANDLE, WS-NOTHING, WS-PARENT.

But nothing is returned in WS-PARENT. After the "C$XML" USING CXML-GET-PARENT, the RETURN-CODE is zero. Why is that giving me a Return Code of zero? I was expecting "EmailInfo" to be returned in WS-PARENT. What am I doing wrong? Or is there something wrong with the XML file?



C:\temp\test.pdf
New Mail
RECEIVED
09/06/2007
15:34
Me
You




0 Likes
10 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

If your whole XML file is encompassed inside the EmailInfo element then that is the element you are located on when you parse the file. Being the document-root, it has no parent - only children.

Use XML-GET-DATA to obtain the EmailInfo element, not XML-GET-PARENT.

Have you got the sample XML program working that posted for you earlier? This really helped me get my head around C$XML.

Ian
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

Thanks, Ian.

You helped me figure out the FilePath issue I was having before.
But now since I learned that the XML file that I will be parsing could contain multiple "parents", that is why I need to get the parent. I need to know the "parent" because processing will be different for each parent in the XML file.

I could have a XML file like this


blah, blah
blah, blah



blah, blah,
blah, blah


So I need to get the parent and process for the email information, and then I need to get parent and process for the contact information.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

In the previous post, the , , , are all indented. Not sure why when I post the message, it does not leave the indents.

Ian, I just looked at the Example you posted, again, and I doing like you have, but I am not getting "EmailInfo" to come back.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

OK, I was able to get "EmailInfo" from the XML file by only using the XML-GET-DATA, right after the XML-PARSE-FILE

But, I still have the issue of being able to get the next parent .
Not sure how I would do that????
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

A well-formed XML file has only one document root, which will be the first element returned when you parse the file. So I'm thinking your file should look like this:-

 
    blah, blah
    blah, blah
 

 
    blah, blah,
    blah, blah
 
Then the EmailInfo and ContactInfo are all children of your document-root element. Is this how your file really is?

If you're working in Windows, try to open your XML file in a browser to see if your XML file is well-formed or not (there are probably other ways). If you're working in Linux, use xml_parser or xmlwf to check your file. Parsing the sample you provided returns errors using all of these test methods.

In my example above, I would do something like this:-
CALL "C$XML" USING CXML-PARSE-FILE, filename.
MOVE RETURN-CODE    TO  XML-PARSER-HANDLE.
CALL "C$XML" USING CXML-GET-FIRST-CHILD, XML-PARSER-HANDLE.
MOVE RETURN-CODE    TO  WS99-XML-ELEMENT-HANDLES(1).
perform until return-code = zero
    CALL "C$XML" using cxml-get-data,
                    WS99-XML-ELEMENT-HANDLES(1),
                    WS99-XML-DATA-NAME,
                    WS99-XML-DATA-ITEM
    EVALUATE WS99-XML-DATA-NAME
        WHEN "EmailInfo"
            Perform Email-info
        WHEN "ContactInfo"
            perform Contact-Info
    End-Evaluate
    CALL "C$XML" USING CXML-GET-NEXT-SIBLING,
                          WS99-XML-ELEMENT-HANDLES(1)
    MOVE RETURN-CODE    TO  WS99-XML-ELEMENT-HANDLES(1)
end-perform.

Email-info.
**** Similar code - but the handles and subscripts change...
CALL "C$XML" USING CXML-GET-FIRST-CHILD, WS99-XML-ELEMENT-HANDLES(1).
MOVE RETURN-CODE    TO  WS99-XML-ELEMENT-HANDLES(2).
perform until return-code = zero
    CALL "C$XML" using cxml-get-data,
                    WS99-XML-ELEMENT-HANDLES(2),
                    WS99-XML-DATA-NAME,
                    WS99-XML-DATA-ITEM
    EVALUATE WS99-XML-DATA-NAME
        WHEN "Filepath"
            do stuff
        WHEN "subject"
            do stuff
    End-Evaluate
    CALL "C$XML" USING CXML-GET-NEXT-SIBLING,
                          WS99-XML-ELEMENT-HANDLES(2)
    MOVE RETURN-CODE    TO  WS99-XML-ELEMENT-HANDLES(2)
end-perform.


By keeping track of XML handles in an array like this (more like a stack really - and the idea was pinched from Acucorp's example program), you remove the need for moving up-and-down the tree using the PARENT options in C$XML. When you've exhausted all of the XML elements at the subscript-2 level (i.e. return-code is now zero, you go back up to the handle at subscript-1 and get the next sibling at that level - simply by referencing it by the handle you've saved in your array.

I hope I'm helping and not confusing you 🙂

Ian
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

Ian,

Thank you for all your knowledge and wisdom.

I wasn't exactly sure what the XML file was going to look like until this week. There is another company that is going the XML file creation part of my project, so I don't have any control over what the XML looks like.

However, the XML file that I am working off, EmailInfo is the document root and Contacts is under it.


-
blah, blah (indented 3 spaces)
blah blah (indented 3 spaces)
- (indented 3 spaces)
- (indented 5 spaces)
blah, blah (indented 7 spaces)
blah, blah (indented 7 spaces)
(indented 5 spaces)
(indented 3 spaces)



So I guess I need to get the children of the child ContactInfo, of the child Contacts?
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

Yep, that sounds about right to me...
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

Below is the XML file I have and I still cannot get the second Contact Info.
I have tried Child By Name, Sibling by Name, Next Child. Whatever I do, I just cannot get the second Contact Info information. Any other suggestions?





L


T








LT@lsystems.com










(800) 886-9999

(610) 555-1414
(610) 555-1414






You


Are








you@systems.com










(610) 555-1212








0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

Try working through the file using Acucorp's sample program. This should show you the correct sequence of C$XML commands to use. Do you need the compiled object?

My approach would be:-
1. Parse the file giving you the handle to . Save this handle as A.
2. Get-1st-Child of A should return the handle to . Save this handle as B.
3. Get-1st-Child of B should return the handle to . Save this handle as C.
3. Get-Next-Sibling of C (in a loop) should return the remaining items.
4. When you've exhausted the children of "B", Get-Next-Sibling of "B" should return the next .

Saving the handles in an array is the trick (I've found). Occurrence 1 always points to the primary element (handle A above), occurrence 2 always points to the children of the primary, occurrence 3 always refers to grand-children, etc.

Ian
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Trying to Get the Parent from an XML file

Ian,

I really appreciate all your help.
I will be posting another issue really soon.
Feel free to give your expertise.

Thank you, thank you, thank you.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.