Accessing Objects when using XML

Welcome Forums General PowerShell Q&A Accessing Objects when using XML

Viewing 6 reply threads
  • Author
    Posts
    • #221334
      Participant
      Topics: 1
      Replies: 3
      Points: 21
      Rank: Member

      Hi everybody,

      this is my first question (I hope not too stupid…) in this forum. I have looked for a solution here and in the web, but did not find anything.

      When I create an object from an XML file like

      $Main = [xml] (Get-Content -Path ($Source + $XMLMainDocument))

      I can access the XML File like this

      $BZRFromXML = $Main.archive.content.document.extension.property.value[0]

      And here is my problem:

      If there is no such node in the object, I do not get a valid value. No Problem. If there are several ‘value’-nodes, I can access them as an array. No problem either. But if there is exactly _one_ ‘value’, my code using the array notation does not work, because it is a single object, not an array. Thus, using [] results in an error.

      I have circumvented the problem by writing two code branches for no/several and _one_ value. That is a lot of work and bad for maintenance. I am sure, there is a better way, I just don’t know it.

      Can you help me?

      Thanks for your time

    • #221343
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      You can cast a single element to an array if needed … example:

      $Variable = 'singleElement'
      $Variable[0]
      @($Variable)[0]
    • #221349
      Participant
      Topics: 1
      Replies: 3
      Points: 21
      Rank: Member

      OK, I just create an array from my single variable. Can I do that in a complex object like in my example? And how do I address the fact that I simply don’t know if my variable is single or array?

      Thanks again.

    • #221352
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      OK, I just create an array from my single variable.

      In case you create an array with a sinlge element in it.

      Can I do that in a complex object like in my example?

      Try it.

      And how do I address the fact that I simply donโ€™t know if my variable is single or array?

      I don’t understand this question.

    • #221373
      Participant
      Topics: 1
      Replies: 3
      Points: 21
      Rank: Member

      Sorry, bad question…

      I have code like this :

      for ($k = 0; $k -le ($MyRecordsCount -1); $k++)
      {
      $date = $Staple.LedgerImport.consolidate.cashLedger.date[$k]
      $amount = $Staple.LedgerImport.consolidate.cashLedger.amount[$k]
      $accountNo = $Staple.LedgerImport.consolidate.cashLedger.accountNo[$k]
      $buCode = $Staple.LedgerImport.consolidate.cashLedger.buCode[$k]
      #DoSomething

      }

      $Staple is an object created from an XML file. If $MyRecordsCount is exactly 1, this Code does not work, because none of the above objects is an array. Therefore, right now I make a second branch by asking if $MyRecordsCount is 1 where the code is

      {
      $date = $Staple.LedgerImport.consolidate.cashLedger.dateย 
      $amount = $Staple.LedgerImport.consolidate.cashLedger.amount
      $accountNo = $Staple.LedgerImport.consolidate.cashLedger.accountNo
      $buCode = $Staple.LedgerImport.consolidate.cashLedger.buCode
      #DoSomething

      }

      I have the feeling that this is not a smart way to do programming ๐Ÿ˜‰

      But I also have not found out how to apply your first answer to solve my problem.

    • #221382
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      When you post code or error messages or sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE”, in the “Visual” view you can use the format template “Preformatted”.
      Thanks in advance.

      To use your example … that could be one way:

      for ($k = 0; $k -le ($MyRecordsCount - 1); $k++) {
          $date = @($Staple.LedgerImport.consolidate.cashLedger.date)[$k]
          $amount = @($Staple.LedgerImport.consolidate.cashLedger.amount)[$k]
          $accountNo = @($Staple.LedgerImport.consolidate.cashLedger.accountNo)[$k]
          $buCode = @($Staple.LedgerImport.consolidate.cashLedger.buCode)[$k]
          #DoSomething
      }

      I hope you are aware of the fact that you do not need this “complex” loop. In Powershell we have the foreach loop. I don’t know the structure of your XML but when you have an array of elements you can easily iterate over the elements of that array with foreach like this:

      $arrayOfElements = 'Eins','Zwei','Drei','Vier'
      
      foreach($SingleElement in $arrayOfElements){
          "Do something with '$($SingleElement)'"
      }
    • #221388
      Participant
      Topics: 1
      Replies: 3
      Points: 21
      Rank: Member

      _Now_ I got the picture. Thank you very much!

      Thank you also for the hint with foreach. I know it and use it in my software, but in this case, I need $k in the DoSomething-section anyway.

      No doubt that your rank is Community Hero ๐Ÿ˜‰ ๐Ÿ™‚

Viewing 6 reply threads
  • You must be logged in to reply to this topic.