简体   繁体   English

如何从Java文件中以字符串形式读取XML内容

[英]How to read XML content as String from a file in Java

I get a text file from my source which has the below content in single line. 我从源代码中获取了一个文本文件,该文本文件中的以下内容为一行。

<employees><employee><id>101</id><name>Lokesh Gupta</name><title>Author</title></employee><employee><id>102</id><name>Brian Lara</name><title>Cricketer</title></employee></employees>

In my code, I have to read each employee data as String. 在我的代码中,我必须将每个员工数据读取为String。 Eg: <employee><id>101</id><name>Lokesh Gupta</name><title>Author</title></employee> as a string and <employee><id>102</id><name>Brian Lara</name><title>Cricketer</title></employee> as another string. 例如: <employee><id>101</id><name>Lokesh Gupta</name><title>Author</title></employee>作为字符串, <employee><id>102</id><name>Brian Lara</name><title>Cricketer</title></employee>作为另一个字符串。 When I print the content on console, it has to print <employee><id>101</id><name>Lokesh Gupta</name><title>Author</title></employee> . 当我在控制台上打印内容时,它必须打印<employee><id>101</id><name>Lokesh Gupta</name><title>Author</title></employee> Could you please let me know how to do this? 你能让我知道怎么做吗?

Generally the file I get from my source consists of 100+ million employee details in single line and I have to read all those employee details as individual String and store that in other file. 通常,我从源代码中获取的文件包含一行中的100+百万个员工详细信息,我必须将所有这些员工详细信息读取为单独的String并将其存储在其他文件中。 Since the file size is huge, I tried using SAX parser and with that I am able to parse xml content but unable to read the entire data as string. 由于文件很大,因此我尝试使用SAX解析器,并且能够解析xml内容,但无法将整个数据读取为字符串。

I tried using SAX Parser and default handler to read this content. 我尝试使用SAX Parser和默认处理程序读取此内容。 But in startElement and EndElement methods, I have to write my logic to append < and > , < and /> respectively. 但是在startElement和EndElement方法中,我必须编写逻辑以分别追加<></> I want to know better way of reading this instead of writing logic to append the Angular brackets. 我想知道更好的阅读方法,而不是编写逻辑来添加尖括号。

One way to do this is to use Streaming feature of JaxB which effectivly uses SAX underneath. 一种方法是使用JaxB的流功能,该功能有效地使用了下面的SAX。 Here is an example: 这是一个例子:

 // create JAXBContext for the primer.xsd
        JAXBContext context = JAXBContext.newInstance("primer");

        Unmarshaller unmarshaller = context.createUnmarshaller();

        // purchase order notification callback
        final PurchaseOrders.Listener orderListener = new PurchaseOrders.Listener() {
            public void handlePurchaseOrder(PurchaseOrders purchaseOrders, PurchaseOrderType purchaseOrder) {
                System.out.println("this order will be shipped to "
                        + purchaseOrder.getShipTo().getName());
            }
        };

        // install the callback on all PurchaseOrders instances
        unmarshaller.setListener(new Unmarshaller.Listener() {
            public void beforeUnmarshal(Object target, Object parent) {
                if(target instanceof PurchaseOrders) {
                    ((PurchaseOrders)target).setPurchaseOrderListener(orderListener);
                }
            }

            public void afterUnmarshal(Object target, Object parent) {
                if(target instanceof PurchaseOrders) {
                    ((PurchaseOrders)target).setPurchaseOrderListener(null);
                }
            }
        });

        // create a new XML parser
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader reader = factory.newSAXParser().getXMLReader();
        reader.setContentHandler(unmarshaller.getUnmarshallerHandler());

        for (String arg : args) {
            // parse all the documents specified via the command line.
            // note that XMLReader expects an URL, not a file name.
            // so we need conversion.
            reader.parse(new File(arg).toURI().toString());
        }
    }
}

It is taken straight from jaxB samples in jaxb/ri https://github.com/javaee/jaxb-v2/blob/master/jaxb-ri/samples/src/main/samples/streaming-unmarshalling/src/Main.java 它直接取自jaxb / ri https://github.com/javaee/jaxb-v2/blob/master/jaxb-ri/samples/src/main/samples/streaming-unmarshalling/src/Main.java中的 jaxB示例

The PurchaseOrders.Listener interface is : PurchaseOrders.Listener接口是:

public static interface Listener {
        void handlePurchaseOrder(PurchaseOrders purchaseOrders, PurchaseOrderType purchaseOrder);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM