解析器在java中解析未知的XML Schema [英] Parser to parse unknown XML Schema in java

查看:50
本文介绍了解析器在java中解析未知的XML Schema的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试理解 stackoverflow 中的所有其他答案.但我无法将这些答案与我的问题联系起来.

I tried understanding all other answers in stackoverflow.But I am not able to relate those answers to my question.

当我调用网络服务时,我得到response.我通过 response.getData();(包含结果的数据表的 XML.) (返回类型字符串) 获取模式.我们不知道我们在那个 XML 中得到了什么数据.

When I call a web service, I get response. I get schema by response.getData();(The XML of the data table containing the results.) (return type String). We don't know what data we get in that XML.

我需要使用第 3 方解析器,以便当我将上述字符串提供给该解析器中的一个方法时,它应该返回该 XML 中的所有元素,然后我可以打印所需的元素.

I need to use a 3rd party parser, so that when I give the above string to one method in that parser it should return all the elements in that XML and then I can print the required elements.

我不想自己开始解析 XML.有没有办法做到这一点?(它甚至有任何意义吗?).对不起,如果我完全错了.(使用 Axis2/eclipse)(已编辑)

I don't want to start parsing the XML myself. Is there a way I can do this? (Does it even make any sense?). Sorry If I am totally wrong. (using Axis2/eclipse) (Edited)

添加我已经尝试过的代码.

Adding the code I've tried already.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
NodeList nodeList = null;


        try {
            String xml = res2.getResult().getRawData();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document document = db.parse(new ByteArrayInputStream(xml.getBytes()));
            nodeList = document.getElementsByTagName("PhoneNumber");
            NamedNodeMap attrib = document.getAttributes();
            for (int i = 0; i < attrib.getLength(); i++) {
                String nodeName = attrib.item(i).getNodeName();
                //nodeName
                String nodeValue = attrib.item(i).getNodeValue();
            }

但我不确定电话号码是否带有该标签或其他名称.我们也不知道我们有多少标签.

But I am not sure if the PhoneNumber is with that tag or other name. Also we don't know how many tags we have.

谢谢,使用 SyamS 的代码,我能够从 xml 打印所有节点和相应的值.现在我想将它存储到一个哈希图中,键为节点名称,节点值在列表中.

Thanks, Using the code by SyamS, I am able to print all the nodes and corresponding values from xml. Now I want to store that into a hashmap with key as node name and node values in a list.

示例 XML:

<Docs>
<Doc>
<Id>12</Id>
<Phone>1234</Phone>
</Doc>
<Doc>
<Id>147</Id>
<Phone>12345</Phone>
<Locked>false</Locked>
<BID>2</BID>
<DocId>8</DocId>
<Date>2014-02-04T12:18:50.063-07:00</Date>
<Urgent>false</Urgent>
</Doc>
</Docs>

推荐答案

您无需为此寻找第三方库.您可以简单地使用 xpath 识别所有叶节点并读取值(以及属性).例如

You need not go for a third party library for this. you could simply identify all leaf nodes using xpath and read the value (as well as attributes). For example

public static Map<String, List<String>> parseXml(String xml) throws XMLStreamException {
    StringBuilder content = null;
    Map<String, List<String>> dataMap = new HashMap<>();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    InputStream stream = new ByteArrayInputStream(xml.getBytes());
    XMLStreamReader reader = factory.createXMLStreamReader(stream);

    while (reader.hasNext()) {
        int event = reader.next();

        switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                content = new StringBuilder();
                break;

            case XMLStreamConstants.CHARACTERS:
                if (content != null) {
                    content.append(reader.getText().trim());
                }
                break;

            case XMLStreamConstants.END_ELEMENT:
                if (content != null) {
                    String leafText = content.toString();
                    if(dataMap.get(reader.getLocalName()) == null){
                        List<String> values = new ArrayList<>();
                        values.add(leafText);
                        dataMap.put(reader.getLocalName(), values);
                    } else {
                        dataMap.get(reader.getLocalName()).add(leafText);
                    }
                }
                content = null;
                break;

            case XMLStreamConstants.START_DOCUMENT:
                break;
        }

    }

    return dataMap;
}

这篇关于解析器在java中解析未知的XML Schema的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆