Sample code for 30+ languages & platforms
PowerBuilder

XML Sort by Content

See more XML Examples

Demonstrates the SortByContent method.

The input XML, available at http://www.chilkatsoft.com/data/fruitSort2.xml, is this:

<root>
    <fruits>
        <apples>
            <apple>fuji</apple>
            <apple>gala</apple>
            <apple>granny smith</apple>
            <apple>honeycrisp</apple>
            <apple>mcintosh</apple>
        </apples>
        <fruit>banana</fruit>
        <fruit>blackberry</fruit>
        <fruit>blueberry</fruit>
        <fruit>orange</fruit>
        <fruit>pear</fruit>
    </fruits>
</root>

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Xml
oleobject loo_XSortRoot
long li_BAscending

li_Success = 0

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_Xml
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  The sample input XML is available at http://www.chilkatsoft.com/data/fruitSort2.xml
li_Success = loo_Xml.LoadXmlFile("fruitSort2.xml")
if li_Success <> 1 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Xml
    return
end if

//  Sort the direct children under the "fruits" node by tag:
loo_XSortRoot = loo_Xml.FindChild("fruits")

//  Sort in ascending order.
li_BAscending = 1
loo_XSortRoot.SortByContent(li_BAscending)

//  Show the result:
Write-Debug loo_Xml.GetXml()

//  Note:  The "apples" node contains child nodes, but its 
//  text content is 0-length (empty).  Therefore, when sorting in
//  ascending order, it will be positioned before the direct
//  children containing non-empty content.

//  <root>
//      <fruits>
//          <apples>
//              <apple>granny smith</apple>
//              <apple>gala</apple>
//              <apple>fuji</apple>
//              <apple>mcintosh</apple>
//              <apple>honeycrisp</apple>
//          </apples>
//          <fruit>banana</fruit>
//          <fruit>blackberry</fruit>
//          <fruit>blueberry</fruit>
//          <fruit>orange</fruit>
//          <fruit>pear</fruit>
//      </fruits>
//  </root>

//  Sort the direct children under the "apples" node:
li_Success = loo_XSortRoot.FindChild2("apples")

loo_XSortRoot.SortByContent(li_BAscending)
Write-Debug loo_Xml.GetXml()

//  <root>
//      <fruits>
//          <apples>
//              <apple>fuji</apple>
//              <apple>gala</apple>
//              <apple>granny smith</apple>
//              <apple>honeycrisp</apple>
//              <apple>mcintosh</apple>
//          </apples>
//          <fruit>banana</fruit>
//          <fruit>blackberry</fruit>
//          <fruit>blueberry</fruit>
//          <fruit>orange</fruit>
//          <fruit>pear</fruit>
//      </fruits>
//  </root>

destroy loo_XSortRoot


destroy loo_Xml