How to remove the white spaces between tags in XML

后端 未结 7 910
太阳男子
太阳男子 2020-12-06 18:00

I created an XML document using Java in my android application. I have to call a web service in my application and pass this XML as an argument there. But my problem is ther

7条回答
  •  借酒劲吻你
    2020-12-06 18:17

    You can create a copy of all nodes in a document, and trims the nodeValue of each node if it exists.

    const copyChildrenNodesWithoutWhiteSpace = (document) => {
      const clone = document.cloneNode();
    
      for (const child of document.childNodes) {
        const childCopy = copyChildrenNodesWithoutWhiteSpace(child);
        clone.appendChild(childCopy);
    
        if (childCopy.nodeValue) {
          childCopy.nodeValue = childCopy.nodeValue.trim();
        }
      }
    
      return clone;
    };
    
    const result = copyChildrenNodesWithoutWhiteSpace(anyDocument);
    

提交回复
热议问题