Extract measurements from XML nodes
I'm parsing large XMLs using Java 8 and XmlPath 1.0. I want to extract, name of the Test, his measured values and the outcome (Passed or Failed).
Each Test can have many TestResult which contains one of the two types of limits:
- SingleLimit, which will have only one < Limit comparator="XYZ">
LimitPair which will have always two limits
<tr:Test ID = "282" name = "DIP1-8 High">
<tr:Extension>
<ts:TSStepProperties>
...
</ts:TSStepProperties>
</tr:Extension>
<tr:Outcome value = "Passed" /> <!-- value -->
<tr:TestResult ID = "xyz" name = "TC 8.7.4 - Current breaker output J10:1-2"> <!-- name -->
<tr:TestLimits>
<tr:Limits>
<c:LimitPair operator = "AND">
<c:Limit comparator = "GE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "2.8" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
<c:Limit comparator = "LE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "3.5" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
</c:LimitPair>
</tr:Limits>
</tr:TestLimits>
</tr:TestResult>
</tr:Test>
Currently I'm using these paths to extract PairLimit measurements and to create String containing values.
My question is how I should write code/xPaths to take care of possible many TestResults inside one Test.
I assumed at the beginning that Test can have only PairLimit or SingleLimit, which was wrong.
My current code extract all values correctly, but assigned measurements are incorrect when there are many TestResults inside Test.
For instance, if Test ID = 1 contains 3 (three) TestResults then in the final String containing measurements, I will have values from first Test inside second, because it will "override" the values.
private ArrayList<String> preparePairLimitPaths() {
final ArrayList<String> list = new ArrayList<>();
list.add("//Test[TestResult//LimitPair]/@name");
list.add("//Test/TestResult[TestLimits//LimitPair]/TestData/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test[TestResult//TestLimits//LimitPair]/Outcome/@value");
return list;
}
for (String expr : preparePairLimitPaths) {
try {
final NodeList evaluate = (NodeList) xPath.evaluate(expr, parse, XPathConstants.NODESET);
for (int i = 0; i < evaluate.getLength(); i++) {
final String textContent = evaluate.item(i).getTextContent();
if (textContent != null && !textContent.isEmpty()) {
stringBuilder.append(textContent).append(";");
}
}
stringBuilder.append("@@@");
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
java xml java-8 extract xpath-1.0
add a comment |
I'm parsing large XMLs using Java 8 and XmlPath 1.0. I want to extract, name of the Test, his measured values and the outcome (Passed or Failed).
Each Test can have many TestResult which contains one of the two types of limits:
- SingleLimit, which will have only one < Limit comparator="XYZ">
LimitPair which will have always two limits
<tr:Test ID = "282" name = "DIP1-8 High">
<tr:Extension>
<ts:TSStepProperties>
...
</ts:TSStepProperties>
</tr:Extension>
<tr:Outcome value = "Passed" /> <!-- value -->
<tr:TestResult ID = "xyz" name = "TC 8.7.4 - Current breaker output J10:1-2"> <!-- name -->
<tr:TestLimits>
<tr:Limits>
<c:LimitPair operator = "AND">
<c:Limit comparator = "GE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "2.8" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
<c:Limit comparator = "LE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "3.5" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
</c:LimitPair>
</tr:Limits>
</tr:TestLimits>
</tr:TestResult>
</tr:Test>
Currently I'm using these paths to extract PairLimit measurements and to create String containing values.
My question is how I should write code/xPaths to take care of possible many TestResults inside one Test.
I assumed at the beginning that Test can have only PairLimit or SingleLimit, which was wrong.
My current code extract all values correctly, but assigned measurements are incorrect when there are many TestResults inside Test.
For instance, if Test ID = 1 contains 3 (three) TestResults then in the final String containing measurements, I will have values from first Test inside second, because it will "override" the values.
private ArrayList<String> preparePairLimitPaths() {
final ArrayList<String> list = new ArrayList<>();
list.add("//Test[TestResult//LimitPair]/@name");
list.add("//Test/TestResult[TestLimits//LimitPair]/TestData/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test[TestResult//TestLimits//LimitPair]/Outcome/@value");
return list;
}
for (String expr : preparePairLimitPaths) {
try {
final NodeList evaluate = (NodeList) xPath.evaluate(expr, parse, XPathConstants.NODESET);
for (int i = 0; i < evaluate.getLength(); i++) {
final String textContent = evaluate.item(i).getTextContent();
if (textContent != null && !textContent.isEmpty()) {
stringBuilder.append(textContent).append(";");
}
}
stringBuilder.append("@@@");
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
java xml java-8 extract xpath-1.0
add a comment |
I'm parsing large XMLs using Java 8 and XmlPath 1.0. I want to extract, name of the Test, his measured values and the outcome (Passed or Failed).
Each Test can have many TestResult which contains one of the two types of limits:
- SingleLimit, which will have only one < Limit comparator="XYZ">
LimitPair which will have always two limits
<tr:Test ID = "282" name = "DIP1-8 High">
<tr:Extension>
<ts:TSStepProperties>
...
</ts:TSStepProperties>
</tr:Extension>
<tr:Outcome value = "Passed" /> <!-- value -->
<tr:TestResult ID = "xyz" name = "TC 8.7.4 - Current breaker output J10:1-2"> <!-- name -->
<tr:TestLimits>
<tr:Limits>
<c:LimitPair operator = "AND">
<c:Limit comparator = "GE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "2.8" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
<c:Limit comparator = "LE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "3.5" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
</c:LimitPair>
</tr:Limits>
</tr:TestLimits>
</tr:TestResult>
</tr:Test>
Currently I'm using these paths to extract PairLimit measurements and to create String containing values.
My question is how I should write code/xPaths to take care of possible many TestResults inside one Test.
I assumed at the beginning that Test can have only PairLimit or SingleLimit, which was wrong.
My current code extract all values correctly, but assigned measurements are incorrect when there are many TestResults inside Test.
For instance, if Test ID = 1 contains 3 (three) TestResults then in the final String containing measurements, I will have values from first Test inside second, because it will "override" the values.
private ArrayList<String> preparePairLimitPaths() {
final ArrayList<String> list = new ArrayList<>();
list.add("//Test[TestResult//LimitPair]/@name");
list.add("//Test/TestResult[TestLimits//LimitPair]/TestData/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test[TestResult//TestLimits//LimitPair]/Outcome/@value");
return list;
}
for (String expr : preparePairLimitPaths) {
try {
final NodeList evaluate = (NodeList) xPath.evaluate(expr, parse, XPathConstants.NODESET);
for (int i = 0; i < evaluate.getLength(); i++) {
final String textContent = evaluate.item(i).getTextContent();
if (textContent != null && !textContent.isEmpty()) {
stringBuilder.append(textContent).append(";");
}
}
stringBuilder.append("@@@");
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
java xml java-8 extract xpath-1.0
I'm parsing large XMLs using Java 8 and XmlPath 1.0. I want to extract, name of the Test, his measured values and the outcome (Passed or Failed).
Each Test can have many TestResult which contains one of the two types of limits:
- SingleLimit, which will have only one < Limit comparator="XYZ">
LimitPair which will have always two limits
<tr:Test ID = "282" name = "DIP1-8 High">
<tr:Extension>
<ts:TSStepProperties>
...
</ts:TSStepProperties>
</tr:Extension>
<tr:Outcome value = "Passed" /> <!-- value -->
<tr:TestResult ID = "xyz" name = "TC 8.7.4 - Current breaker output J10:1-2"> <!-- name -->
<tr:TestLimits>
<tr:Limits>
<c:LimitPair operator = "AND">
<c:Limit comparator = "GE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "2.8" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
<c:Limit comparator = "LE">
<!-- value -->
<c:Datum nonStandardUnit = "V" value = "3.5" xsi:type="ts:TS_double" flags = "0x0000"/>
</c:Limit>
</c:LimitPair>
</tr:Limits>
</tr:TestLimits>
</tr:TestResult>
</tr:Test>
Currently I'm using these paths to extract PairLimit measurements and to create String containing values.
My question is how I should write code/xPaths to take care of possible many TestResults inside one Test.
I assumed at the beginning that Test can have only PairLimit or SingleLimit, which was wrong.
My current code extract all values correctly, but assigned measurements are incorrect when there are many TestResults inside Test.
For instance, if Test ID = 1 contains 3 (three) TestResults then in the final String containing measurements, I will have values from first Test inside second, because it will "override" the values.
private ArrayList<String> preparePairLimitPaths() {
final ArrayList<String> list = new ArrayList<>();
list.add("//Test[TestResult//LimitPair]/@name");
list.add("//Test/TestResult[TestLimits//LimitPair]/TestData/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test/TestResult/TestLimits/Limits/LimitPair/Limit[*]/Datum/@value");
list.add("//Test[TestResult//TestLimits//LimitPair]/Outcome/@value");
return list;
}
for (String expr : preparePairLimitPaths) {
try {
final NodeList evaluate = (NodeList) xPath.evaluate(expr, parse, XPathConstants.NODESET);
for (int i = 0; i < evaluate.getLength(); i++) {
final String textContent = evaluate.item(i).getTextContent();
if (textContent != null && !textContent.isEmpty()) {
stringBuilder.append(textContent).append(";");
}
}
stringBuilder.append("@@@");
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
java xml java-8 extract xpath-1.0
java xml java-8 extract xpath-1.0
asked Nov 25 '18 at 21:33
nanomader94nanomader94
198212
198212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can just iterate over each Test and then iterate over each TestResult and then put the logic with TestLimits etc.
NodeList allTests = (NodeList) xPath.evaluate("/xml/Test", xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < tests.getLength(); i++) {
Element singleTest = (Element) tests.item(i);
// Here, you can extract some values from your test like:
// testOutcome = xPath.evaluate("Outcome/@value", singleTest);
NodeList testResults = (NodeList) xPath.evaluate("TestResult",test, XPathConstants.NODESET);
for (int j=0; j<testResults.getLength(); j++) {
// Now you can iterate over all your testResults from test
// testResultName = xPath.evaluate("@name",testResults.item(j)));
}
}
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472230%2fextract-measurements-from-xml-nodes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can just iterate over each Test and then iterate over each TestResult and then put the logic with TestLimits etc.
NodeList allTests = (NodeList) xPath.evaluate("/xml/Test", xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < tests.getLength(); i++) {
Element singleTest = (Element) tests.item(i);
// Here, you can extract some values from your test like:
// testOutcome = xPath.evaluate("Outcome/@value", singleTest);
NodeList testResults = (NodeList) xPath.evaluate("TestResult",test, XPathConstants.NODESET);
for (int j=0; j<testResults.getLength(); j++) {
// Now you can iterate over all your testResults from test
// testResultName = xPath.evaluate("@name",testResults.item(j)));
}
}
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
add a comment |
You can just iterate over each Test and then iterate over each TestResult and then put the logic with TestLimits etc.
NodeList allTests = (NodeList) xPath.evaluate("/xml/Test", xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < tests.getLength(); i++) {
Element singleTest = (Element) tests.item(i);
// Here, you can extract some values from your test like:
// testOutcome = xPath.evaluate("Outcome/@value", singleTest);
NodeList testResults = (NodeList) xPath.evaluate("TestResult",test, XPathConstants.NODESET);
for (int j=0; j<testResults.getLength(); j++) {
// Now you can iterate over all your testResults from test
// testResultName = xPath.evaluate("@name",testResults.item(j)));
}
}
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
add a comment |
You can just iterate over each Test and then iterate over each TestResult and then put the logic with TestLimits etc.
NodeList allTests = (NodeList) xPath.evaluate("/xml/Test", xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < tests.getLength(); i++) {
Element singleTest = (Element) tests.item(i);
// Here, you can extract some values from your test like:
// testOutcome = xPath.evaluate("Outcome/@value", singleTest);
NodeList testResults = (NodeList) xPath.evaluate("TestResult",test, XPathConstants.NODESET);
for (int j=0; j<testResults.getLength(); j++) {
// Now you can iterate over all your testResults from test
// testResultName = xPath.evaluate("@name",testResults.item(j)));
}
}
You can just iterate over each Test and then iterate over each TestResult and then put the logic with TestLimits etc.
NodeList allTests = (NodeList) xPath.evaluate("/xml/Test", xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < tests.getLength(); i++) {
Element singleTest = (Element) tests.item(i);
// Here, you can extract some values from your test like:
// testOutcome = xPath.evaluate("Outcome/@value", singleTest);
NodeList testResults = (NodeList) xPath.evaluate("TestResult",test, XPathConstants.NODESET);
for (int j=0; j<testResults.getLength(); j++) {
// Now you can iterate over all your testResults from test
// testResultName = xPath.evaluate("@name",testResults.item(j)));
}
}
answered Nov 26 '18 at 8:19
mkuligowskimkuligowski
866820
866820
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
add a comment |
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
Works like a charm. Thanks a lot
– nanomader94
Nov 26 '18 at 8:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53472230%2fextract-measurements-from-xml-nodes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown