2017-10-09 94 views
0

我需要解析一些我拥有XSD的XML内容。一般来说,这是直截了当的。但是,在一个特定情况下,XML有时包含XML名称空间,有时它不包含。此外,要求XML命名空间并不实际,因为提供的XML来自多个来源。所以我坚持试图找到解决办法。如何解析可能有或没有命名空间的XML内容?

如上所述,我有XML的XSD,并且我使用XJC(来自JAXB)从XSD生成相应的XML实体类。

示例XML包含命名空间:

<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns="http://www.w3.org/namespace/"> 
    <foo id="123> 
     <bar>value</bar> 
    </foo> 
</root> 

示例XML不含命名空间:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <foo id="123> 
     <bar>value</bar> 
    </foo> 
</root> 

正如你所看到的,XML的内容的结构相同 - 唯一的区别是xmlxs属性在root实体上。

我的代码如下:

URI uri = <URI of XML file> 
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
factory.setNamespaceAware(true); 
Node node = builder.parse(uri.toString()); // Parsing succeeds, ie. the XML is valid. 
JAXBContext context = JAXBContext.newInstance("com.example.xml"); 
Unmarshaller parser = context.createUnmarshaller(); 
// Next line succeeds or fails, depending on presence of namespace 
Object object = parser.unmarshal(node); 

的XML总是成功解析成Node。如果xmlns属性存在于XML中,那么整个过程将正常完成,并且我会收到类(使用XJC生成)的一个实例。从那里我可以访问FooBar对象。

如果xmlns属性不存在,那么解组失败,出现以下异常:

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"root"). 
    Expected elements are <{http://www.w3.org/namespace/}root>, 
    <{http://www.w3.org/namespace/}foo>, 
    <{http://www.w3.org/namespace/}bar> 

我试图用unmarmshalling by declared type有限的成功。具体来说,解组完成没有错误。但是,由此产生的Root类不包含任何FooBar对象。

该代码,这包括改变最后一行:

Object object = parser.unmarshal(node, Root.class); 

我试图与“命名空间感知”标志设置为false解组,但这个错误而失败。

我想过在解组之前,如果它没有一个名字空间的话,我想添加一个名字空间到node。但是API似乎不允许这样做。

我的另一个想法是有两套生成的类,每种情况(即名称空间,没有命名空间)。然而,这看起来像是一团糟。

所以我卡住了?有什么建议么?或者我试图做不可能的事情?

回答

1

您可以使用XML过滤器。这里是我的例子,删除它存在的ns。

package testjaxb; 

import java.io.StringReader; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Unmarshaller; 
import javax.xml.transform.sax.SAXSource; 
import org.xml.sax.Attributes; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.XMLReader; 
import org.xml.sax.helpers.XMLFilterImpl; 
import org.xml.sax.helpers.XMLReaderFactory; 

public class MarshalWithFilter { 

    public static void main(String[] args) throws Exception { 
     String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 
       + "<root xmlns=\"http://www.w3.org/namespace/\">\n" 
       + " <foo id=\"123\">\n" 
       + "  <bar>value</bar>\n" 
       + " </foo>\n" 
       + "</root>"; 

     String xmlStringWithoutNs = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" 
       + "<root>\n" 
       + " <foo id=\"123\">\n" 
       + "  <bar>value</bar>\n" 
       + " </foo>\n" 
       + "</root>"; 

     Root r = (Root) unmarshal(xmlString); 
     System.out.println("root.." + r.getFoo().getId()); 
     System.out.println("root.." + r.getFoo().getBar()); 
     r = (Root) unmarshal(xmlStringWithoutNs); 
     System.out.println("root.." + r.getFoo().getId()); 
     System.out.println("root.." + r.getFoo().getBar()); 
    } 

    private static Root unmarshal(String sampleXML) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(Root.class); 
     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     XMLReader reader = XMLReaderFactory.createXMLReader(); 
     IngoreNamespaceFilter nsFilter = new IngoreNamespaceFilter(); 
     nsFilter.setParent(reader); 
     StringReader stringReader = new StringReader(sampleXML); 
     InputSource is = new InputSource(stringReader); 
     SAXSource source = new SAXSource(nsFilter, is); 
     System.out.println("" + sampleXML); 
     return (Root) unmarshaller.unmarshal(source); 
    } 
} 

class IngoreNamespaceFilter extends XMLFilterImpl { 

    public IngoreNamespaceFilter() { 
     super(); 
    } 

    @Override 
    public void startDocument() throws SAXException { 
     super.startDocument(); 
    } 

    @Override 
    public void startElement(String arg0, String arg1, String arg2, 
      Attributes arg3) throws SAXException { 

     super.startElement("", arg1, arg2, arg3); //Null uri 
    } 

    @Override 
    public void endElement(String arg0, String arg1, String arg2) 
      throws SAXException { 

     super.endElement("", arg1, arg2); //null url 
    } 

    @Override 
    public void startPrefixMapping(String prefix, String url) 
      throws SAXException { 
     //ignore namessopace 

    } 

} 

而且下面是POJO:

package testjaxb; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement(name="root") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Root 
{ 
    private Foo foo; 


    public Foo getFoo() 
    { 
     return foo; 
    } 

    public void setFoo (Foo foo) 
    { 
     this.foo = foo; 
    } 


} 

package testjaxb; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 


@XmlAccessorType(XmlAccessType.FIELD) 
public class Foo 
{ 
    @XmlAttribute 
    private String id; 

    private String bar; 

    public String getId() 
    { 
     return id; 
    } 

    public void setId (String id) 
    { 
     this.id = id; 
    } 

    public String getBar() 
    { 
     return bar; 
    } 

    public void setBar (String bar) 
    { 
     this.bar = bar; 
    } 


} 
+0

ŧ花时间把你的解决方案放在一起。我已尽我所能在自己的环境中复制它,但它对我无效。我的命名空间过滤器正在被使用,但是当调用super.startElement(“”,...);'时,我在我的问题中记录了与'Root相同的'UnmarshalException'。我一直在调试它一段时间没有运气。 – dave

+0

@dave我使用你的示例粘贴的问题进行了测试。你有不同的XML失败。 – Optional

+0

你的解决方案奏效了,所以我必须做些傻事。我睡在上面,回顾了不同之处。我从一个'URI'开始,而不是'String',但我可以看到我曾经为此工作过。这留下了注释的POJO。你的手编码和我的生成。然后它碰到了我,我正在使用从XSD生成的类_包含namespace_,而不是没有。毕竟,我们从XML中删除命名空间,所以类也不应该有。切换到正确的生成类集后,一切正常。再次感谢。 – dave