0

I have a long multiline string (its in a file that I can load into a string), lets take this as example:

<response status="success" code="19">
  <result total-count="1" count="1">
    <deviceconfig>
      <system>
        <type>
          <static/>
        </type>
      </system>
      <setting>
    </deviceconfig>
  </result>
</response>
<response status="success" code="19">
  <result total-count="1" count="1">
    <network>
      <interface>
        <ethernet/>
      </interface>
      <profiles>
        <monitor-profile>
          <entry name="default">
            <interval>3</interval>
            <action>wait-recover</action>
          </entry>
        </monitor-profile>
      </profiles>
    </network>
  </result>
</response>

I need to extract the system subset of this and have it in a string with the following content taken from above example:

      <system>
        <type>
          <static/>
        </type>
      </system>

In my use case both the original sample is much much larger as well as the text inside the system is a little bit larger

0

ok, i found a solution to do this:

preg_match_all('/(<system>.*<\/system>)/ms', file_get_contents('./myinputfile'), $xmlinit);
$output=$xmlinit[0][0]
file_put_contents('./myoutputfile', $output );

Recommendations for improvements are welcome!

  • Use XML parser functions instead ? ie simplexml_load_file – Anonymous Jan 19 at 16:40
  • yes, I actually use simplexml once I extracted that subpart, the problem is that the complete file has some errors according to simplexml and wont load into it... that original file is not modified however as it is exactly the way it needs to be for a program that reads this file. I extract this subpart to continue extracting its values inside of it with simplexml unfortunately i dont even know how to debug or see the exact errors the simplexml_load_file creates, i have enabled logging in php but dont see logs being created... but thats a different issue – Questi Jan 19 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.