开发者

readXmlfile from java

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");
            }
        }

        fin开发者_运维问答ally {
            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


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.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜