Skip to content

Commit

Permalink
refactor: parse response by xml path in http collect
Browse files Browse the repository at this point in the history
  • Loading branch information
Carpe-Wang committed Aug 24, 2024
1 parent bad28bc commit 4d31312
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,38 @@ private void parseResponseBySiteMap(String resp, List<String> aliasFields,

private void parseResponseByXmlPath(String resp, List<String> aliasFields, HttpProtocol http,
CollectRep.MetricsData.Builder builder) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builderFactory = factory.newDocumentBuilder();

Document document = builderFactory.parse(new ByteArrayInputStream(resp.getBytes(StandardCharsets.UTF_8)));
document.getDocumentElement().normalize();

for (String alias : aliasFields) {
NodeList nodeList = document.getElementsByTagName(alias);
if (nodeList.getLength() > 0) {
Node node = nodeList.item(0);
String value = node.getTextContent();

CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
valueRowBuilder.addColumns(value);
builder.addValues(valueRowBuilder.build());
} else {
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
builder.addValues(valueRowBuilder.build());
}
}

} catch (Exception e) {
String errorMsg = "Error parsing XML response: " + e.getMessage();
log.error(errorMsg, e);
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg(errorMsg);
}
}


private void parseResponseByJsonPath(String resp, List<String> aliasFields, HttpProtocol http,
CollectRep.MetricsData.Builder builder, Long responseTime) {
List<Object> results = JsonPathParser.parseContentWithJsonPath(resp, http.getParseScript());
Expand Down

0 comments on commit 4d31312

Please sign in to comment.