0

I'm struggling with problem, what probably has simple code solution, but was not able to find it for Javascript.

I want to clean XML from empty elements using DOMParser or whatever JS tools.

Example input:

<xml>
  <test>1</test>
  <test2></test2>
  <test3></test3>
  <test4>
    <blah>1</blah>
    <bruh></bruh>
  </test4>
</xml>

Output:

<xml>
  <test>1</test>
  <test4>
    <blah>1</blah>
 </test4>
</xml>

Edit: First suggestion from #4800808 were OK for that example, but it does not work, when XML has attributes. What about this:

<xml>
  <test>1</test>
  <test2></test2>
  <test3></test3>
  <test4 attr="value">
    <blah>1</blah>
    <bruh></bruh>
  </test4>
</xml>

1 Answer 1

0

If you have the XML stored in a string x, call x.replace(/<[\S]+?><\/[\S]+?>/, ""). source: https://stackoverflow.com/a/3129760/4800808

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it really works for simple XML, but when i tested it on my complex example, I discovered that it does not process nodes, what contain attributes in parent nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.