java - 使用 Java 从 XML 中提取数据

标签 java xml xml-parsing

我有以下 XML 代码:

<CampaignFrameResponse
  xmlns="http://Qsurv/api"
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Message>Success</Message>
  <Status>Success</Status>
  <FrameHeight>308</FrameHeight>   
  <FrameUrl>http://delivery.usurv.com?Key=a5018c85-222a-4444-a0ca-b85c42f3757d&amp;ReturnUrl=http%3a%2f%2flocalhost%3a8080%2feveningstar%2fhome</FrameUrl> 
</CampaignFrameResponse>

我想做的是提取节点并将它们分配给一个变量。因此,例如,我有一个名为 FrameHeight 的变量,其中包含值 308

这是我目前的 Java 代码:

private void processNode(Node node) {
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node currentNode = nodeList.item(i);
       if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            //calls this method for all the children which is Element
            LOG.warning("current node name: " + currentNode.getNodeName());
            LOG.warning("current node type: " + currentNode.getNodeType());
            LOG.warning("current node value: " + currentNode.getNodeValue());
            processNode(currentNode);
       }
    }

}

这会打印出节点名称、类型和值,但是将每个值分配给适当命名的变量的最佳方法是什么?例如 int FrameHeight = 308?

这是我更新的代码,其中 nodeValue 变量不断返回 null:

processNode(Node node) {
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node currentNode = nodeList.item(i);
    if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
        //calls this method for all the children which is Element
        String nodeName = currentNode.getNodeName();
        String nodeValue = currentNode.getNodeValue();
        if(nodeName.equals("Message")) {
            LOG.warning("nodeName: " + nodeName); 
            message = nodeValue;
            LOG.warning("Message: " + message); 
        } 
        else if(nodeName.equals("FrameHeight")) {
            LOG.warning("nodeName: " + nodeName); 
            frameHeight = nodeValue;
            LOG.warning("frameHeight: " + frameHeight);
        }
        processNode(currentNode);
    }
}

最佳答案

您可以使用 DOMSAXPull-Parser,但最好使用以下 API。

- JAXP 和 JAXB

- 蓖麻

例如:DOM 解析

DocumentBuilderFactory odbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder odb =  odbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(xml));
            Document odoc = odb.parse(is);
            odoc.getDocumentElement().normalize ();    // normalize text representation
            System.out.println ("Root element of the doc is " + odoc.getDocumentElement().getNodeName());
            NodeList LOP = odoc.getElementsByTagName("response");

                Node FPN =LOP.item(0);
                try{
                if(FPN.getNodeType() == Node.ELEMENT_NODE)
                    {

                    Element token = (Element)FPN;

                    NodeList oNameList1 = token.getElementsByTagName("user_id");
                    Element firstNameElement = (Element)oNameList1.item(0);
                    NodeList textNList1 = firstNameElement.getChildNodes();
                    this.setUser_follower_id(Integer.parseInt(((Node)textNList1.item(0)).getNodeValue().trim()));
                    System.out.println("#####The Parsed data#####");
                    System.out.println("user_id : " + ((Node)textNList1.item(0)).getNodeValue().trim());
                    System.out.println("#####The Parsed data#####");

关于java - 使用 Java 从 XML 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837430/

相关文章:

java - BigInteger 使用什么算法将整数转换为字节数组

php - simplexml_load_string() 不会读取标签中带有 "soap:"的 soap 响应

java - 使用xml解析查找word文档中的隐式分页符

php - 读取 xml 文件中的特定标签,而不使用任何类型的 php 库或扩展

c++ - 如何编译库,然后将其与 ITK 一起使用

c# - 使用 C# 使用 XDocument 解析 XML

java - 如何设置 TableColumnValue JavaFX TableView

java - Java中有互斥锁吗?

java - final 在构造函数和属性中

java - String.contains 找到一个子字符串但没有找到 Pattern.matches?