【问题标题】:How can I get the value inside the xml tag?如何获取 xml 标签内的值?
【发布时间】:2012-12-21 11:53:51
【问题描述】:

我正在尝试获取 xml 文件的值。

<p1>
    <cts>Pq44</cts>
    <cts>qw44</cts>
</p1>

P1.JAVA

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class P1 {
    private List<Cts> cts;

     public P1() {
         cts = new ArrayList<cts>();
     }  
     public List<cts> getcts() {
        return cts;
     }
     public void setcts(List<cts> cts) {
        this.cts = cts;
     }
}

CTS.JAVA

public class CTS {

    private String ct;

    // Getter and setter for ct.

}

我的Main.java

try {            
    File file = new File("D:\\Bye.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    P1 p = (P1) jaxbUnmarshaller.unmarshal(file);

    List<CTS> cts =  p.getCTS();
    // Size of list coming right `2`
    for (CTS c : cts) {
          System.out.println(CTS2.getCT());
    }
} catch (JAXBException e) {
    e.printStackTrace();
}

当我运行 main.java 时,它会打印:

null
null

【问题讨论】:

    标签: java xml xml-parsing jaxb


    【解决方案1】:

    我认为你的循环应该是这样的

    for (CTS c : cts) {
        System.out.println(c.getCt());
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用@XmlValue 并执行以下操作:

      CTS

      import javax.xml.bind.annotation.*;
      
      @XmlAccessorType(XmlAccessType.FIELD)
      public class CTS {
      
          @XmlValue
          private String ct;
      
          public String getCt() {
              return ct;
          }
      
          public void setCt(String ct) {
              this.ct = ct;
          }
      
      }
      

      P1

      import java.util.*;
      import javax.xml.bind.annotation.*;
      
      @XmlRootElement
      @XmlAccessorType(XmlAccessType.FIELD)
      public class P1 {
      
          private List<CTS> cts;
      
          public P1() {
              cts = new ArrayList<CTS>();
          }
      
          public List<CTS> getCts() {
              return cts;
          }
      
          public void setCts(List<CTS> cts) {
              this.cts = cts;
          }
      
      }
      

      p1.xml

      <p1>
          <cts>Pq44</cts>
          <cts>qw44</cts>
      </p1>
      

      应用

      import java.io.File;
      import java.util.List;
      
      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.JAXBException;
      import javax.xml.bind.Unmarshaller;
      
      /**
       * Hello world!
       *
       */
      public class App 
      {
          public static void main( String[] args ) throws JAXBException
          {
              System.out.println( "Hello World!" );
              String filePath = ".\\p1.xml";
      
              File file = new File(filePath);
              JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);
      
              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
              P1 p = (P1) jaxbUnmarshaller.unmarshal(file);
      
              List<CTS> cts =  p.getCts();
              // Size of list coming right `2`
              for (CTS c : cts) {
                    System.out.println(c.getCt());
              }
          }
      }
      

      输出

      Hello World!
      Pq44
      qw44
      

      【讨论】:

        【解决方案3】:

        我已经做了一些工作。我正在发布我的代码,它将基于 XML 文件工作。

        p1.xml

        <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <p1>
            <cts>Pq44</cts>
            <cts>qw44</cts>
        </p1>
        

        P1.java

        import java.util.ArrayList;
        
        import javax.xml.bind.annotation.XmlRootElement;
        
        @XmlRootElement(name = "p1")
        public class P1 {
        
            private ArrayList<String> cts;
            public ArrayList<String> getCts() {
                return cts;
            }
            public void setCts(ArrayList<String> cts) {
                this.cts = cts;
            }
        }
        

        TestApp.java

        import java.io.File;
        import java.util.List;
        
        import javax.xml.bind.JAXBContext;
        import javax.xml.bind.JAXBException;
        import javax.xml.bind.Unmarshaller;
        
        /**
         * Hello world!
         *
         */
        public class App 
        {
            public static void main( String[] args ) throws JAXBException
            {
                System.out.println( "Hello World!" );
                String filePath = ".\\p1.xml";
        
                File file = new File(filePath);
                JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);
        
                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                P1 p = (P1) jaxbUnmarshaller.unmarshal(file);
        
                List<String> cts =  p.getCts();
                // Size of list coming right `2`
                for (String c : cts) {
                      System.out.println(c);
                }
            }
        }
        

        试试这个方法会很好用的。

        输出

        Hello World!
        Pq44
        qw44
        

        【讨论】:

        猜你喜欢
        • 2012-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        相关资源
        最近更新 更多