English 中文(简体)
java st st
原标题:java stax get string of inner node
  • 时间:2010-11-10 21:19:25
  •  标签:
  • java
  • xml
  • stax

我将java和XMLStreamReader改为xml。 我想 gr笑某些内 no。

XML:

<example>
   <ignoreMe>
      <bla></bla>
   </ignoreMe>
   <getMe>
      <data></data>
   </getMe>
</example>

我只想能够把整个内部的“Me node”带入“Sting”。 IE:

   <getMe>
      <data></data>
   </getMe>

这里是我的事情,但我 st:

XMLStreamReader parser = factory.createXMLStreamReader(new FileReader(file)); 
for (int event = parser.next();  
event != XMLStreamConstants.END_DOCUMENT;
event = parser.next()) {

    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        if (parser.getLocalName().equals("GetMe")) {
             //??????????????????
最佳回答

上周,我开始学习StaX,但如果你仍在寻求解决你的问题,这可能有助于你:

    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlsr;
    String resultat="";
    boolean isGetme=false;

    try {
        xmlsr = xmlif.createXMLStreamReader(new FileReader("lib/toto.xml"));
        int eventType;
        while (xmlsr.hasNext()) {
            eventType = xmlsr.next();
            switch (eventType) {
                case XMLStreamConstants.START_ELEMENT:
                    if(xmlsr.getLocalName().equals("getMe")){
                        isGetme=true;
                    }
                    if(isGetme){
                        resultat+="<"+xmlsr.getLocalName()+">";
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    if(isGetme){
                        resultat+=xmlsr.getText();
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    if(xmlsr.getLocalName().equals("getMe")){
                        resultat+="</"+xmlsr.getLocalName()+">";
                        isGetme=false;
                    }
                    if(isGetme && !xmlsr.getLocalName().equals("getMe")){
                        resultat+="</"+xmlsr.getLocalName()+">";
                    }
                    break;
                default:
                    break;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    System.out.println(resultat);

This code will get you all the inner nodes and text contents, and store it into a String. I.E:

<getMe>
      <data>test</data>
</getMe>

然而,这部法律“格外”而且肯定不是你所期望的最佳解决办法,因此,联索行动可能更适合你们的需要。

问题回答

rel=“nofollow”>this

检查 par子的方法

HTH

这将使你掌握内部文本,你可以采用正常方法人工添加起首和终止名称。 既然XML是一种严格的标准,你将予以罚款。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签