Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

java - How to declare namespaces only on root element of XML

Is there a way to declare all namespaces on the root element of a XML string serialized with Jackson?

This is what I expect:

<?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>

This is what I'm getting:

<?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>

I'm annotating all variables with @JacksonXmlProperty to set localName and namespace, as well as using @JacksonXmlRootElement in the root class to set localName and namespace of the root element. But the namespace of the inner elements is declared only in the first element that uses the namespace. How to declare all at the root element at the top, as shown in the example?

question from:https://stackoverflow.com/questions/65920332/how-to-declare-namespaces-only-on-root-element-of-xml

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...