如何解析可能有或没有命名空间的XML内容?

我需要解析一些我有XSD的XML内容。 总的来说,这是直截了当的。 但是,在一个特定情况下,XML有时包含XML命名空间,有时则不包含。 此外,要求XML命名空间并不实际,因为提供的XML来自多个来源。 所以我一直试图找到解决方法。

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

示例XML包括命名空间:

   value   

不包含命名空间的示例XML:

    value   

如您所见,XML内容在结构上是相同的 – 唯一的区别是root实体上的xmlxs属性。

我的代码如下:

 URI uri =  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 。 如果XML中存在xmlns属性,则整个过程正常完成,并且我收到com.example.xml.Root类的实例(使用XJC生成)。 从那里我可以访问FooBar对象。

如果缺少xmlns属性,则解组将失败,并出现以下exception:

 javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"root"). Expected elements are , ,  

我尝试通过声明的类型进行非组合 ,但成功有限。 具体来说,解组完成没有错误。 但是,生成的Root类不包含任何FooBar对象。

此代码涉及将最后一行更改为:

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

我尝试将“namespace aware”标志设置为false解组,但是失败并出现错误。

在解组之前,我已经考虑过在node添加名称空间(如果它没有名称空间)。 然而,API似乎不允许这样做。

我的另一个想法是拥有两组生成的类,每种情况一个(即命名空间,没有命名空间)。 然而,这似乎是一个很大的问题。

所以我被卡住了? 有什么建议? 或者我正在尝试做什么不可能?

您可以使用XMLfilter。 这是我的例子,删除它所在的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 = "\n" + "\n" + " \n" + " value\n" + " \n" + ""; String xmlStringWithoutNs = "\n" + "\n" + " \n" + " value\n" + " \n" + ""; 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 } } 

以下是Pojos:

 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; } }