Hey all I am trying to see if I can read an XML file and only gather the tags that have the date formatted like YYYY-MM-DD.
Here is an online example: https://repl.it/repls/MedicalIgnorantEfficiency
Here is an example of my xml to parse:
<?xml version="1.0" encoding="UTF-8"?>
<ncc:Message xmlns:ncc="http://blank/1.0.6"
xmlns:cs="http://blank/1.0.0"
xmlns:jx="http://blank/1.0.0"
xmlns:jm="http://blank/1.0.0"
xmlns:n-p="http://blank/1.0.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://blank/1.0.6/person person.xsd">
<ncc:DataSection>
<ncc:PersonResponse>
<!-- Message -->
<cs:CText cs:type="No">NO WANT</cs:CText>
<jm:CaseID>
<!-- OEA -->
<jm:ID>ABC123</jm:ID>
</jm:CaseID>
<jx:PersonName>
<!-- NAM -->
<jx:GivenName>Arugula</jx:GivenName>
<jx:MiddleName>Pibb</jx:MiddleName>
<jx:SurName>Atari</jx:SurName>
</jx:PersonName>
<!-- DOB -->
<ncc:PersonBirthDateText>1948-05-11</ncc:PersonBirthDateText>
<jx:PersonDetails>
<!-- SXC -->
<jx:PersonSSN>
<jx:ID/>
</jx:PersonSSN>
</jx:PersonDetails>
<n-p:Activity>
<!--DOZ-->
<jx:ActivityDate>1996-04-04</jx:ActivityDate>
<jx:HomeAgency xsi:type="cs:Organization">
<!-- ART -->
<jx:Organization>
<jx:ID>ZR5981034</jx:ID>
</jx:Organization>
</jx:HomeAgency>
</n-p:Activity>
<jx:PersonName>
<!-- DOB Newest -->
<ncc:BirthDateText>1993-05-12</ncc:BirthDateText>
<ncc:BirthDateText>1993-05-13</ncc:BirthDateText>
<ncc:BirthDateText>1993-05-14</ncc:BirthDateText>
<jx:IDDetails xsi:type="cs:IDDetails">
<!-- SMC Checker -->
<jx:SSNID>
<jx:ID/>
</jx:SSNID>
</jx:IDDetails>
</jx:PersonName>
</ncc:PersonResponse>
</ncc:DataSection>
</ncc:Message>
I am looking to want to get the date value(s) and the comment above those date values. So something like this for the example xml above:
Comment: < !-- DOB --> (ncc:DataSection/ncc:PersonResponse)
Date: 1948-05-11 (ncc:DataSection/ncc:PersonResponse/ncc:PersonBirthDateText)
.
Comment: < !-- DOZ --> (ncc:DataSection/ncc:PersonResponse/n-p:Activity)
Date: 1996-04-04 (ncc:DataSection/ncc:PersonResponse/n-p:Activity/jx:ActivityDate)
.
Comment: < !-- DOB Newest --> (ncc:DataSection/ncc:PersonResponse/jx:PersonName)
Date:
1993-05-12 (ncc:DataSection/ncc:PersonResponse/jx:PersonName/ncc:BirthDateText) 1993-05-13 (ncc:DataSection/ncc:PersonResponse/jx:PersonName/ncc:BirthDateText) 1993-05-14 (ncc:DataSection/ncc:PersonResponse/jx:PersonName/ncc:BirthDateText)
The code I am trying to do this with is:
public static void xpathNodes() throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
File file = new File(base_);
XPath xPath = XPathFactory.newInstance().newXPath();
//String expression = "//*[not(*)]";
String expression = "([0-9]{4})-([0-9]{2})-([0-9]{2})";
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(file);
document.getDocumentElement().normalize();
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(getXPath(nodeList.item(i)));
}
}
private static String getXPath(Node node) {
Node parent = node.getParentNode();
if (parent == null) {
return node.getNodeName();
}
return getXPath(parent) + "/" + node.getNodeName();
}
public static void main(String[] args) throws Exception {
xpathNodes();
}
I know the Regex (([0-9]{4})-([0-9]{2})-([0-9]{2})) works as I have used it in Notepad++ and it works just fine there finding the dates within the opened xml file.
I am currently getting the error:
Exception in thread "main" javax.xml.transform.TransformerException: A location path was expected, but the following token was encountered: [
This doesn't even take in consideration the comments yet.
Any help would be great!
xPath.compile(expression)as if it were. Seematches()within a predicate if you're using an XPath 2.0 processor, or adopt two stages of XPath + Java regex processing if you're limited to XPath 1.0.