如何仅在 XML 的根元素上声明命名空间

问题描述

有没有办法在用 Jackson 序列化的 XML 字符串的根元素上声明所有命名空间?

这是我所期望的:

<?xml version="1.0" encoding="UTF-8"?>
<RequestMessage xmlns="Example1" xmlns:m="Example2">
   <Header>
      <Verb>get</Verb>
      <Noun>MeterReadings</Noun>
      <User>
         <UserID>admin</UserID>
      </User>
      <Password>password</Password>
   </Header>
   <Request>
      <m:Readings>
         <m:Device>
            <m:mRID>12345</m:mRID>
         </m:Device>
         <m:ReadingType>A</m:ReadingType>
      </m:Readings>
   </Request>
</RequestMessage>

这是我得到的:

<?xml version="1.0" encoding="UTF-8"?>
<RequestMessage xmlns="Example1">
   <Header>
      <Verb>get</Verb>
      <Noun>readings</Noun>
      <User>
         <UserID>admin</UserID>
      </User>
      <Password>password</Password>
   </Header>
   <Request>
      <m:Readings xmlns:m="Example2">
         <m:Device>
            <m:mRID>12345</m:mRID>
         </m:Device>
         <m:ReadingType>A</m:ReadingType>
      </m:Readings>
   </Request>
</RequestMessage>

我使用@JacksonXmlProperty 注释所有变量以设置localName 和命名空间,并在根类中使用@JacksonXmlRootElement 设置根元素的localName 和命名空间。但是内部元素的命名空间仅在使用命名空间的一个元素中声明。如何在顶部的根元素声明all,如示例所示?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)