I will have files like:Parsing and catching is probably the most safe approach.
I will need to parse to xml:
However it is not valid xml since it is two roots. How can I detect
that I have two roots. Currently I am only seeing this when parse
fails ( see below) And message is too general to know there are
multiple ( in this case 2) roots. If I can detect multi roots I can
split string and parse each and add them together later ( I think).
On 2/23/2021 12:22 PM, mike wrote:
I will have files like:Parsing and catching is probably the most safe approach.
I will need to parse to xml:
However it is not valid xml since it is two roots. How can I detect
that I have two roots. Currently I am only seeing this when parse
fails ( see below) And message is too general to know there are
multiple ( in this case 2) roots. If I can detect multi roots I can
split string and parse each and add them together later ( I think).
If you want a string hack then regex may be the easiest approach.
For inspiration:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class XmlSniffer {
private static Pattern either = Pattern.compile("(\\<[\\w-]+.*?\\>)|(\\</[\\w-]+\\>)");
private static Pattern start = Pattern.compile("\\<[\\w-]+.*?\\>");
private static Pattern end = Pattern.compile("\\</[\\w-]+\\>");
private static Pattern both = Pattern.compile("\\<[\\w-]+.*?/\\>");
public static boolean check(String xmlstr) {
Matcher e = either.matcher(xmlstr);
int depth = 0;
boolean first = true;
while(e.find()) {
String piece = e.group();
if(start.matcher(piece).matches()) {
if(depth == 0) {
if(first) {
first = false;
} else {
return false;
}
}
if(!both.matcher(piece).matches()) {
depth++;
}
} else if(end.matcher(piece).matches()) {
depth--;
}
}
return true;
}
private static void test(String xmlstr) {
System.out.println(xmlstr);
System.out.println(check(xmlstr));
}
public static void main(String[] args) {
test("<a/>");
test("<a>xxxx</a>");
test("<a>\r\n <b>xxxx</b>\r\n</a>");
test("<a>\r\n <b/>\r\n</a>");
test("<a>xxxx</a>\r\n<c>yyyy</c>");
test("<a>\r\n <b>xxxx</b>\r\n</a>\r\n<c>yyyy</c>");
test("<a>\r\n <b/>\r\n</a>\r\n<c>yyyy</c>");
test("<a>xxxx</a>\r\n<c/>");
test("<a>\r\n <b>xxxx</b>\r\n</a>\r\n<c/>");
test("<a>\r\n <b/>\r\n</a>\r\n<c/>");
test("<a/>\r\n<c/>");
}
}
Arne
Hi,
I will have files like:
<get>
<filter>
<configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
<system>
<security>
<user-params>
<local-user>
<user>
<user-name /> <!-- selection node in context A -->
</user>
</local-user>
</user-params>
</security>
</system>
</configure>
<state xmlns="urn:nokia.com:sros:ns:yang:sr:state">
<system>
<security>
<user-params>
<local-user>
<user>
<attempted-logins /> <!-- selection node in context B -->
</user>
</local-user>
</user-params>
</security>
</system>
</state>
</filter>
</get>
I will need to parse to xml:
<configure xmlns="urn:nokia.com:sros:ns:yang:sr:conf">
<system>
<security>
<user-params>
<local-user>
<user>
<user-name /> <!-- selection node in context A -->
</user>
</local-user>
</user-params>
</security>
</system>
</configure>
<state xmlns="urn:nokia.com:sros:ns:yang:sr:state">
<system>
<security>
<user-params>
<local-user>
<user>
<attempted-logins /> <!-- selection node in context B -->
</user>
</local-user>
</user-params>
</security>
</system>
</state>
However it is not valid xml since it is two roots. How can I detect that I have two roots. Currently I am only seeing this when parse fails ( see below) And message is too general to know there are multiple ( in this case 2) roots.
If I can detect multi roots I can split string and parse each and add them together later ( I think).
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 293 |
Nodes: | 16 (3 / 13) |
Uptime: | 215:36:46 |
Calls: | 6,620 |
Calls today: | 2 |
Files: | 12,169 |
Messages: | 5,317,547 |