有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用java从下面的xml代码中提取值?

如何使用java从下面的xml代码中提取值? 在这里,我想使用java提取to、from、body和thread值。 这里的总代码以字符串形式显示

<message to="-105608156545@chat.facebook.com/Smack" 
from="-105465454665906545@chat.facebook.com" 
type="chat">
<body>sai</body>
<thread>NNLWF1</thread>
<active xmlns="http://jabber.org/protocol/chatstates" />
</message>

共 (4) 个答案

  1. # 1 楼答案

    下面是一个使用data projection library的完整示例:

    public class DataProjection {
    
        public interface Message {
            @XBRead("/message/@to")
            String getTo();
    
            @XBRead("/message/@from")
            String getFrom();
    
            @XBRead("/message/body")
            String getBody();
    
            @XBRead("/message/thread")
            String getThread();
        }
    
    
        public static void main(String[] args) {
            String xml="<message to=\"-105608156545@chat.facebook.com/Smack\" \n" + 
                    "from=\"-105465454665906545@chat.facebook.com\" \n" + 
                    "type=\"chat\">\n" + 
                    "<body>sai</body>\n" + 
                    "<thread>NNLWF1</thread>\n" + 
                    "<active xmlns=\"http://jabber.org/protocol/chatstates\" />\n" + 
                    "</message>";
    
            Message message = new XBProjector().projectXMLString(xml, Message.class);
    
            System.out.println(message.getFrom());
            System.out.println(message.getTo());
            System.out.println(message.getBody());
            System.out.println(message.getThread());
    
        }
    

    该程序打印出:

    -105465454665906545@chat.facebook.com
    -105608156545@chat.facebook.com/Smack
    sai
    NNLWF1
    
  2. # 2 楼答案

    在java中读取XML的一种方法是:

    import java.io.File;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(new File("yourFile.xml"));
            NodeList nodeList = document.getElementsByTagName("message");
            for(int x=0,size= nodeList.getLength(); x<size; x++) {
                System.out.println(nodeList.item(x).getAttributes().getNamedItem("to").getNodeValue());
            }
        }
    }
    

    希望能有帮助

  3. # 3 楼答案

    一种可能是使用Java内置的XPath功能,例如

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new File("Test.xml"));
    
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression expression = xPath.compile("/message[@from]");
        Node node = (Node)expression.evaluate(document, XPathConstants.NODE);
        System.out.println("From: " + node.getAttributes().getNamedItem("from").getNodeValue());
    
        expression = xPath.compile("/message/body");
        node = (Node)expression.evaluate(document, XPathConstants.NODE);
        System.out.println("Body: " + node.getTextContent());
    
        expression = xPath.compile("/message/thread");
        node = (Node)expression.evaluate(document, XPathConstants.NODE);
        System.out.println("Thread: " + node.getTextContent());
    } catch (ParserConfigurationException | SAXException | IOException | DOMException | XPathExpressionException exp) {
        exp.printStackTrace();
    }
    

    哪个输出

    From: -105465454665906545@chat.facebook.com
    Body: sai
    Thread: NNLWF1
    

    看看:

    欲知详情

  4. # 4 楼答案

    String xmlString="<message>....</message>";//above xml code
    JAXBContext jc = JAXBContext.newInstance( message.class );
    Unmarshaller u = jc.createUnmarshaller();
    message o =(message) u.unmarshal( new StreamSource( new StringReader(xmlString ) ) );
    System.out.println("------getTo-------"+o.getTo());
    System.out.println("------getFrom-------"+o.getFrom());
    System.out.println("------getBody-------"+o.getBody()); 
    System.out.println("------getThread-------"+o.getThread());
    

    和Bean类(消息)代码

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class message {
    
    
    
        public message(){}
    
        private String to;
    
        @XmlAttribute 
        public String getTo() {
            return to;
        }
    
    
    
        public void setTo(String to) {
            this.to = to;
        }
    
    
        @XmlAttribute 
        public String getFrom() {
            return from;
        }
    
    
    
        public void setFrom(String from) {
            this.from = from;
        }
    
    
        @XmlElement  
        public String getBody() {
            return body;
        }
    
    
    
        public void setBody(String body) {
            this.body = body;
        }
    
    
        @XmlElement  
        public String getThread() {
            return thread;
        }
    
    
    
        public void setThread(String thread) {this.thread = thread;
        }
    private String from;
        private String body;
        private String thread;
    
    
    
        public message(String to, String from, String body,String thread ){  
            super();  
            this.to = to;  
            this.from = from;  
            this.body = body;
            this.thread = thread;
    
        }  
    
    }