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

标签 java xsd xml-parsing jaxb

我需要解析一些我拥有 XSD 的 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 内容在结构上是相同的 - 唯一的区别是 root 实体上的 xmlxs 属性。

我的代码如下:

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。如果 XML 中存在 xmlns 属性,则整个过程会正常完成,并且我会收到 com.example.xml.Root 类的实例(该实例是使用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 进行解码,但失败并出现错误。

我考虑过在解码之前向节点添加一个命名空间(如果它没有命名空间)。然而 API 似乎不允许这样做。

我的另一个想法是拥有两组生成的类,每种情况一组(即命名空间,无命名空间)。然而,这似乎太过杂乱。

所以我被困住了?有什么建议么?或者我想做的事情是不可能的?

最佳答案

您可以使用 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:

Root

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


}

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


}

关于java - 如何解析可能有或没有命名空间的 XML 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46637547/

相关文章:

java - 处理异常和错误(错误是可以的)

java - 按日期排序 聚合 mongodb

java - LibGDX:如何实现 Google Play 游戏服务?

java - JAXB 接受重复标签

java - Apache Storm 拓扑上的 Sigar UnsatisfiedLinkError

c# - 在 XmlSerializer 的输出中交换 xsd 和 xsi

xsd - 将 XSD 中的列表读取到 OCaml 和 Coq 中的列表

xml - 用 asp 解析 svg xml

python - 编写 Python 工具将 XML 转换为 Python?

java - 如何解析 SOAP 响应?