Question

I have different xml files with different namespaces eg - oa, ns2, p...etc

I want the namespace to be ignored when converting the xml back to java object. or I want the namespace to be removed from the xml when converting the xml to object.

My sample xml data is

<oa:ApplicationArea>
    <oa:Sender>
        <oa:LogicalId>Volvo</oa:LogicalId>
        <oa:Component>DVLA</oa:Component>
        <oa:Task>ReceiveKeeper</oa:Task>
        <oa:MessageCode>MS1</oa:MessageCode>
        <oa:AuthorizationId>SKARUPAI</oa:AuthorizationId>
        <oa:OrganisationLevel>50</oa:OrganisationLevel>
    </oa:Sender>
    <oa:CreationDateTime>2013-08-31T12:00:00</oa:CreationDateTime>
    <oa:BODId>123456789</oa:BODId>
</oa:ApplicationArea>
<p:DataArea>
    <oa:Sync confirm="Always">
        <oa:SyncCriteria expressionLanguage="XPath">
            <oa:SyncExpression action="Add"></oa:SyncExpression>
        </oa:SyncCriteria>
    </oa:Sync>
    </p:DataArea>

Below is the sample of the code which I am using to convert the above xml to java using Xstream.

public static void main(String[] args) throws Exception {
    FileReader fileReader = new FileReader("C:/kspace/xstream/src/input.out.xml");  // load our xml file
    XStream xstream = new XStream();     // init XStream
    // Determine type of message(Eg. 'EM1') and put the corresponding value from hashmap to a String.
    // Pass the string to xstream.alias(stringnamewhichwasset, xmlRoot.class)

    String interfaceMessageId = "MS1";
    String xmlRootTagName = (String) messages.get(interfaceMessageId);

    xstream.registerConverter(new XMLDateConverter());
    xstream.alias(xmlRootTagName, RootType.class);
    xstream.aliasField("ApplicationArea", RootType.class, "applicationArea");
    xstream.aliasField("DataArea", RootType.class, "dataArea");

    xstream.alias("ApplicationArea", ApplicationArea.class);
    xstream.aliasField("Sender", ApplicationArea.class, "sender");
    xstream.aliasField("CreationDateTime", ApplicationArea.class, "creationDateTime");
    xstream.aliasField("BODId", ApplicationArea.class, "bodId");

    xstream.alias("Sender", Sender.class);
    xstream.aliasField("LogicalId", Sender.class, "logicalId");
    xstream.aliasField("Component", Sender.class, "component");
    xstream.aliasField("Task", Sender.class, "task");
    xstream.aliasField("MessageCode", Sender.class, "messageCode");
    xstream.aliasField("AuthorizationId", Sender.class, "authorizationId");
    xstream.aliasField("OrganisationLevel", Sender.class, "organisationLevel");......
......

Is there any way to do it with Xstream at all?

Was it helpful?

Solution

Finally got the solution after trying using StaxDriver. Here is what I did.

QNameMap qmap = new QNameMap();
qmap.setDefaultNamespace("http://www.somename.com/xyz");
qmap.setDefaultPrefix("");
StaxDriver staxDriver = new StaxDriver(qmap); 
XStream xstream = new XStream(staxDriver);

Creating the xstream object like that will ignore the namespace prefixes while parsing. So far I was successfully able to remove all the namespace prefixes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top