Quote (AbDuCt @ Feb 24 2015 08:37pm)
But if it's just a single line you can just use a search and replace regex.
Assuming the schema is simple and predictable, sure. using XPath isn't that much more code, and you can search by node name reliably. By using regex, you have a lot to take into consideration like namespace prefixes, attributes, etc.
Let's say he has a password in the XML page and wants to mask it.
<Password>hunter2</Password>
Regex to find that node?
Pattern: (<Password>).*(</Password>)
Replace String: $1**********$2
Easy enough. But what if this is a SOAP request for some web service that he is logging, and needs to filter the password so it doesn't get written to disk?
Code
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:Account>AzureDiamond</ns1:Account>
<ns1:Password>hunter2</ns1:Password>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Now we have namespaces to deal with. You could hardcode it, but if the schema ever changes, you would also have to modify the masking logic as well. So, let's make it generic?
Pattern: ((<(\w*:)?Password>).*(</\2Password>)
Replace String: $1**********$3
Password is a required field, but what if the user didn't enter one?
Code
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/1999/XMLSchema">
<SOAP-ENV:Body>
<ns1:Account>AzureDiamond</ns1:Account>
<ns1:Password xsi:nil="true" />
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Now you need a new pattern all together. Unless you want to try and combine them, and trust me...it doesn't end well.
Oh, and none of these patterns took into consideration whitespace which may or may not be present. My point is, using regex to parse XML is never a fun thing. XPath is designed to do this very thing. Just point it at the node you want, and let it handle the parsing.