首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java解析XSI

用Java解析XSI
EN

Stack Overflow用户
提问于 2011-11-12 03:17:57
回答 2查看 413关注 0票数 0

我正在尝试将XML字符串解析为可用于轻松搜索的文档。但是当我遇到某些类型的XML时,它似乎不起作用。文档从不构造,当它遇到XML消息时为空,就像我在底部看到的那样。我的try/catch中的任何东西都不会抛出异常

我的代码目前看起来像这样:

代码语言:javascript
复制
Document convertMessageToDoc(String message){
        Document doc = null;

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(message));

            doc = db.parse(is);
        }
        catch (Exception e) {
            //e.printStackTrace();
            doc = null;
        }

        return doc;
    }

我可以用什么方法来处理这样的东西:

代码语言:javascript
复制
 <ns1:SubmitFNOLResponse xmlns:ns1="http://website.com/">
 <ns1:FNOLReporting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns1:FNOLReporting">
 <ns1:FNOLResponse>
 <ns1:FNOLStatusInfo>
  <ns1:StatusCode>0</ns1:StatusCode> 
  <ns1:StatusMessages /> 
  </ns1:FNOLStatusInfo>
  </ns1:FNOLResponse>
  </ns1:FNOLReporting>
  </ns1:SubmitFNOLResponse>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-12 03:52:13

您的文档不是格式正确的XML。一旦是这样,一切似乎都像预期的那样工作。

代码语言:javascript
复制
String message =
        "<ns1:Prods xmlns:ns1='/foo'>"// xmlns:ns1='uri'>"
                + "<ns1:Prod>"
                + "  <ns1:ProductID>316</ns1:ProductID>"
                + "        <ns1:Name>Blade</ns1:Name>"
                + "</ns1:Prod>"
                + "<ns1:Prod>"
                + "  <ns1:ProductID>317</ns1:ProductID>"
                + " <ns1:Name>LL Crankarm</ns1:Name>"
                + "  <ns1:Color>Black</ns1:Color>"
                + "</ns1:Prod>"
                + "</ns1:Prods>";

Document doc = null;

try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(message));
    doc = db.parse(is);

    NodeList sections = doc.getElementsByTagName("ns1:Prod");
    int numSections = sections.getLength();
    for (int i = 0; i < numSections; i++) {
        Element section = (Element) sections.item(i);
        NodeList prodinfos = section.getChildNodes();
        for (int j = 0; j < prodinfos.getLength(); j++) {
            Node info = prodinfos.item(j);
            if (info.getNodeType() != Node.TEXT_NODE) {
                System.out.println(info.getNodeName() + ": " + info.getTextContent());
            }
        }
        System.out.println("");
    }
} catch (Exception e) {
    e.printStackTrace();
    doc = null;
}

// Outputs

ns1:ProductID: 316
ns1:Name: Blade

ns1:ProductID: 317
ns1:Name: LL Crankarm
ns1:Color: Black
票数 0
EN

Stack Overflow用户

发布于 2011-11-12 03:47:27

看起来你的文档不是“格式良好的”。您需要一个根元素,其中在根元素中有两个兄弟的"ns1:Prod“标记。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8098824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档