Parsing XML

This topic contains 7 replies, has 4 voices, and was last updated by  js 2 weeks, 2 days ago.

  • Author
    Posts
  • #103654

    John Steele
    Participant

    I'm working with DISA STIGs to harden Server 2012 R2. The STIGS use XML files to analyze how compliant the server is. I'm new to working with XML but I'm trying to create a psobject using that XML file and it mostly works. I've created the $xml variable as shown below:

    [xml] $xml = Get-Content -Path stig.xml
    

    The description for each rule in the STIG is a string with XML tags under $xml.benchmark.group.rule.description. I want to pull out those tags and be able to do .description.vulnDiscussion. The example XML is below:

    I've tried using ConvertTo-Xml with no luck. It maintains the object as a string. I've also tried

    [xml] $xml.benchmark.group.rule.description
    

    and I get the error

    Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "This document already has a 'DocumentElement' node."
    

    I'm at a loss on how to convert that string to XML. Any ideas?

  • #103660

    Fredrik Kacsmarck
    Participant

    Any html/xml content need to be stored somewhere else.
    E.g. in Gist and post a link to it.

  • #103714

    John Steele
    Participant

    I've added the XML example using Gist.

  • #103729

    js
    Participant
    $xml.save('whatever.xml')

    It makes it prettier too.

  • #103747

    Rohn Edwards
    Participant

    The error is coming from the fact that there's no root node. For whatever reason, the STIG guide saves that description info as a string ready to be parsed back into XML instead of having it encoded as part of the rest of the checklist's XML. If you want to turn it back into something the [xml] accelerator can parse, you have to wrap it in a parent node. Something like this should work:

    $Description = $xml.benchmark.group[0].rule.description
    [xml] "${Description}" | Select-Object -ExpandProperty root
    

    I named the node 'root' there, but it really doesn't matter since you're just using it as a way to wrap all of the text in the 'description'.

    • #103748

      John Steele
      Participant

      You didn't post anything that wraps that xml with a root node or am I just not understanding here?

      EDIT: Nevermind. I figured out what you were saying.

      I still don't quite understand why this works but it works. Thanks for your help!

    • #103750

      Rohn Edwards
      Participant

      Ha! I always forget that the forum strips out tags. I'm going to repost it below, but replace the brackets around the [root] with less than and greater than signs:

      $Description = $xml.benchmark.group[0].rule.description
      [xml] "[root]${Description}[/root]" | Select-Object -ExpandProperty root
      

      Edit: I see you already figured it out 🙂 Glad it's working!

  • #103757

    js
    Participant

    convertto-xml just turns some other powershell object into an [xml] object. $xml.save() will make xml text, if that's what you're trying to do.

You must be logged in to reply to this topic.