简体   繁体   English

解析响应xml并计算CDATA中的XML元素

[英]Parse response xml and count XML elements in CDATA

I am having a code where I parse the response XML and count the occurrence of the elements. 我有一个代码,我解析响应XML并计算元素的出现。 Is there a way I can parse through the CDATA in the response and count the elements in the CDATA. 有没有一种方法可以解析响应中的CDATA并计算CDATA中的元素。

My current code for parsing the XML. 我当前用于解析XML的代码。

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.parse(new InputSource(new StringReader(response.toString())));
                NodeList list = doc.getElementsByTagName("RESPONSE");
                System.out.println("Total : " + list.getLength());

The sample XML I need to parse through, 我需要解析的示例XML,

 <RESPONSE><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<ID>
  <EMAIL>xxx@yyy</EMAIL>
  <EMAIL>klihf@kjf</EMAIL>
  <EMAIL>ddd@fff</EMAIL>
  <EMAIL>@ddd</EMAIL>
  </ID>
 ]]></RESPONSE>

Thanks 谢谢

You can do it by getting the content of RESPONSE node and continue in a similar way like you already do. 您可以通过获取RESPONSE节点的内容来执行此操作,并以类似于您已经执行的方式继续操作。

For example, by adding this to your code, 例如,通过将其添加到您的代码中,

String content = list.item(0).getTextContent();
Document doc_ = docBuilder.parse(new InputSource(new StringReader(content)));

NodeList listId = doc_.getElementsByTagName("ID");
System.out.println("Total (list ID) : " + listId.getLength());

NodeList listEmail = doc_.getElementsByTagName("EMAIL");
System.out.println("Total (list EMAIL) : " + listEmail.getLength());

Everything between the RESPONSE tags will be treated as a String. RESPONSE标记之间的所有内容都将被视为字符串。 Try: 尝试:

public class Main {
public static void main(String... args) {
    String response = "<RESPONSE><![CDATA[<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "<ID>\n" +
            "  <EMAIL>xxx@yyy</EMAIL>\n" +
            "  <EMAIL>klihf@kjf</EMAIL>\n" +
            "  <EMAIL>ddd@fff</EMAIL>\n" +
            "  <EMAIL>@ddd</EMAIL>\n" +
            "  </ID>\n" +
            " ]]></RESPONSE>";
    String xml = response.substring(response.indexOf("<ID>"), response.lastIndexOf("]]>"));
    Id id = null;
    try {
        JAXBContext jc = JAXBContext.newInstance(Id.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource streamSource = new StreamSource(new StringReader(xml));
        JAXBElement<Id> element = unmarshaller.unmarshal(streamSource, Id.class);
        id = element.getValue();
    } catch (JAXBException ex) {
        ex.printStackTrace();
    }

    System.out.println(id.getEMAIL().size());
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
        "email"
})
@XmlRootElement(name = "ID")
public static class Id {

    @XmlElement(name = "EMAIL")
    private List<String> email;

    public List<String> getEMAIL() {
        if (email == null) {
            email = new ArrayList<>();
        }
        return this.email;
    }
}

} }

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

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