tags:

views:

53

answers:

2

Hello I've written small class in java to read in xml file as a string, not my question is following : how do I append string so it outputs only what is between Physical_Order_List_Array tags

here is a java class :

public static String getFileContent(String fileName)
    {
     BufferedReader br = null;
     StringBuilder sb = new StringBuilder();
     try {
      br = new BufferedReader(new FileReader(fileName));
     try {
      String s;
      while((s = br.readLine()) != null)
      {
       sb.append(s);
       sb.append("\n");
      }
     }

     finally {
      br.close();
     }
     }
     catch (Exception ex) {
      ex.printStackTrace();
     }
     return sb.toString();
    }

here is some of xml file its quite large :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
.....
.....
.....some content
....here is what I only need 
<Physical_Order_List_Array>
....now I need to get this portion of the code, append string to only output what is beetween these tags, including these tags as well
</Physical_Order_List_Array>
.....
.....
.....

Thank you for your answers

+1  A: 

I'd use XPath and use something like //Physical_Order_List_Array. You could parse the XML yourself (a fairly simple state machine would likely do the job in most cases).

mlk
how would I do that ?
c0mrade
http://www.ibm.com/developerworks/library/x-javaxpathapi.html
mlk
+5  A: 

Don't read the file as a string, read it using an XML parser and then use XPath to extract the nodes that you want. I've written articles on both parsing and xpath that will give you the code you need.

Also, define "quite large" -- is it a megabyte or a gigabyte? If the latter, then you'll need a SAX parser, which I don't describe. If the former, the DOM parser is fine.

kdgregory