I need help with something that sounds easy but has given me some trouble.
I have a text file (record.txt) that has a root element 'PatientRecord' and sub tags in it ('first name', 'age', blood type, address etc...) that repeat over and over but with different values since it's a record for each person. I'm only interested in printing out the values in between the tags to a new text file for each person but only for the elements I want. For example with the tags I mentioned above I only want the name and age but not the rest of the info for that patient. How do I print out just those values separated by commas and then go to the next patient? Here is the code I have so far
package patient.records;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class ProcessRecords {
private static final String FILE = "C:\\Users\\Desktop\\records.txt";
private static final String RECORD_START_TAG = "<PatientRecord>";
private static final String RECORD_END_TAG = "</PatientRecord>";
private static final String newFileName = "C:\\Users\\Desktop\\DataFolder\\";
public static void main(String[] args) throws Exception {
String scan;
FileReader file = new FileReader(FILE);
BufferedReader br = new BufferedReader(file);
Writer writer = null;
while ((scan = br.readLine()) != null)
{
if (scan.contains(RECORD_START_TAG)) {
//This is the logic I am missing that will only grab the element values
//between the tags inside of the file
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(newFileName + "Record Data" + ".txt"), "utf-8"));
}
else if (scan.contains(RECORD_END_TAG)) {
writer.close();
writer=null;
}
else {
// only write if writer is not null
if (writer!=null) {
writer.write(scan);
}
}
}
br.close();
}
} //This is the end of my code
The text file (record.txt) I am reading in looks like this:
<PatientRecord> <---first patient record--->
<---XML Schema goes here--->
<Info>
<age>66</age>
<first_name>john</first_name>
<last_name>smith</last_name>
<mailing_address>200 main street</mailing_address>
<blood_type>AB</blood_type>
<phone_number>000-000-0000</phone_number>
</PatientRecord>
<PatientRecord> <---second patient record--->
<---XML Schema goes here--->
<Info>
<age>27</age>
<first_name>micheal</first_name>
<last_name>thompson</last_name>
<mailing_address>123 baker street</mailing_address>
<blood_type>O</blood_type>
<phone_number>111-222-3333</phone_number>
</PatientRecord>
So in theory if I ONLY wanted to print out the values from the tags first name, mailing address, and blood type from this text file for all patients it should look like this:
john, 200 main street, AB
//this line is blank
michael, 123 baker street, O
Thanks for any and all help. If you feel like my code should be modified then I'm all for it. Thank you.
My first gut feeling is to wrap the entire text content around some outer tag and process the text as XML, something like...
<Patients>
<PatientRecord> <---first patient record--->
<Info>
<age>66</age>
<first_name>john</first_name>
<last_name>smith</last_name>
<mailing_address>200 main street</mailing_address>
<blood_type>AB</blood_type>
<phone_number>000-000-0000</phone_number>
</PatientRecord>
...
</Patients>
But there are two problems with this...
One <---first patient record--->
isn't a valid XML comment or text and two, there is no closing </Info>
tag...[sigh]
So, my next thought was, read in each <PatientRecord>
individual, as text, and then process that as XML
....
Here come the problems...we need to remove anything surrounded by <--- ... --->
including the little arrows...There is a lot of assumptions about this, but hopefully we can ignore it...
The next problem is, we need to insert a closing </Info>
tag...
After that, it's all really easy...
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class Test {
private static final String RECORD_START_TAG = "<PatientRecord>";
private static final String RECORD_END_TAG = "</PatientRecord>";
public static void main(String[] args) {
File records = new File("Records.txt");
try (BufferedReader br = new BufferedReader(new FileReader(records))) {
StringBuilder record = null;
String text = null;
while ((text = br.readLine()) != null) {
if (text.contains("<---") && text.contains("--->")) {
String start = text.substring(0, text.indexOf("<---"));
String end = text.substring(text.indexOf("--->") + 4);
text = start + end;
}
if (text.trim().length() > 0) {
if (text.startsWith(RECORD_START_TAG)) {
record = new StringBuilder(128);
record.append(text);
} else if (text.startsWith(RECORD_END_TAG)) {
record.append("</Info>");
record.append(text);
try (ByteArrayInputStream bais = new ByteArrayInputStream(record.toString().getBytes())) {
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression exp = xPath.compile("PatientRecord/Info/first_name");
Node firstName = (Node) exp.evaluate(doc, XPathConstants.NODE);
exp = xPath.compile("PatientRecord/Info/mailing_address");
Node address = (Node) exp.evaluate(doc, XPathConstants.NODE);
exp = xPath.compile("PatientRecord/Info/blood_type");
Node bloodType = (Node) exp.evaluate(doc, XPathConstants.NODE);
System.out.println(
firstName.getTextContent() + ", "
+ address.getTextContent() + ", "
+ bloodType.getTextContent());
} catch (ParserConfigurationException | XPathExpressionException | SAXException ex) {
ex.printStackTrace();
}
} else {
record.append(text);
}
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
Which prints out...
john, 200 main street, AB
micheal, 123 baker street, O
The long and short of it is, go back to the person who gave you this file, slap them, then tell them to put into a valid XML format...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句