有如下一个XML文件:<?xml version="1.0" encoding="gb2312"?>
<operations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
        <result isError="false" type="double">3.0</result>
</operations>
问题:在使用JDOM解析进行读取操作时,老是会出现一个异常:
org.jdom.JDOMException: The name "xsi:noNamespaceSchemaLocation" is not legal for JDOM/XML attributes: Attribute names cannot contain colons.: The name "xsi:noNamespaceSchemaLocation" is not legal for JDOM/XML attributes: Attribute names cannot contain colons.
它的意思我也知道,是说根节点operations中的属性不能有冒号,但xmlns:xsi是取别名,xsi:noNamespaceSchemaLocation是指向约束该xml文件的属性.这2项因为某种原因本人不能舍弃.请问有没有什么方法既能保留这2个属性也能正常解析此XML文件呢?

解决方案 »

  1.   

    接分。我猜一下原因:
    是不是刚定义的namespace xsi不能在当前标签中使用?
      

  2.   

    不管是按字符串解析还是从文件解析,都是没错得啊,不知道楼主的错误从哪来的
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.io.StringReader;
    import java.util.List;import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.Namespace;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;public class ParseXmlString {
    public static void main(String[] args) {
    String xmlstring = "<?xml version='1.0' encoding='utf-8'?>"
    + "<operations xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
    + "xsi:noNamespaceSchemaLocation='test.xsd'>"
    + "<result isError='false' type='double'>3.0</result>"
    + "</operations>"; parse(xmlstring);
    } // 通过Jdom来实现解析
    public static void parse(String xmlstring) {
    SAXBuilder builder = new SAXBuilder();
    try {
    // 创建xml文档对象
    //Document doc = builder.build(new StringReader(xmlstring));
    Document doc = builder.build(new File("e:/11.xml"));
    // 根节点
    Element root = doc.getRootElement();
    Element e = root.getChild("result");
    System.out.println(e.getName()+":"+e.getText());
    } catch (JDOMException e) {
    e.printStackTrace();
    }
    }
    }
      

  3.   

    看看是什么东西。
    DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD