Hi,
I have a program that generates an outputstream with XML-data. With a socket connection, I have to read multiple XML-files from the inputstream.
To read only one XML-file from the inputstream is not a problem, the fact that there are more than one and I have to split up the stream somewhere poses a problem for me.
I have been looking all over the internet for a solution, but I haven't found it yet. Hopefully you can help me..
I already obtained the inputstream here.. but now I have to split it up somewhere to read the multiple files from the stream...
public void run()
{
try{
InputStream is = client.getInputStream();
parse(is);
//System out the data.
/*InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
line = br.readLine();
while ((line = br.readLine()) != null) {
line.trim();
System.out.println(line);
}*/
is.close();
client.close();
}
catch (IOException ioe){
System.err.println(ioe);
}
}
Here I want to make the document..
public static void parse(InputStream xml)
{
try {
//File file = new File("c:\\output.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
byte b[] = new byte[364];
xml.read(b);
ByteArrayInputStream x = new ByteArrayInputStream(b);
Document doc = db.parse(x);
//System.out.println(xml.available());
//System.out.println(new String(b));
//Document doc = db.parse(x);
/*InputSource inStream = new org.xml.sax.InputSource();
inStream.setCharacterStream(new StringReader(xml));*/
/*Document doc = db.parse(xml);
doc.getDocumentElement().normalize();
//System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("MEASUREMENT");
System.out.println("WEERDATA");
System.out.println(" ");
int s = 0;
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
//System.out.println("STN : " + getTagValue("STN", fstElmnt));
System.out.println("STN : " + getTagValue("STN", fstElmnt));
System.out.println("DATE : " + getTagValue("DATE", fstElmnt));
System.out.println("TIME : " + getTagValue("TIME", fstElmnt));
System.out.println("TEMP : " + getTagValue("TEMP", fstElmnt));
System.out.println("DEWP : " + getTagValue("DEWP", fstElmnt));
System.out.println("STP : " + getTagValue("STP", fstElmnt));
System.out.println("SLP : " + getTagValue("SLP", fstElmnt));
System.out.println("VISIB : " + getTagValue("VISIB", fstElmnt));
System.out.println("WDSP : " + getTagValue("WDSP", fstElmnt));
System.out.println("PRCP : " + getTagValue("PRCP", fstElmnt));
System.out.println("SNDP : " + getTagValue("SNDP", fstElmnt));
System.out.println("FRSHTT : " + getTagValue("FRSHTT", fstElmnt));
System.out.println("CLDC : " + getTagValue("CLDC", fstElmnt));
System.out.println("WNDDIR : " + getTagValue("WNDDIR", fstElmnt));
}*/
} catch (Exception e) {
System.out.println("Fout");
}
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}