Transformer removes the namespace incorrectly from xml - java

I have the following xml file which has namespace on NFe and enviNFe tags:
<?xml version="1.0" encoding="UTF-8"?>
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeAutorizacao4">
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<idLote>203364605034338</idLote>
<indSinc>0</indSinc>
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
...
...
But while send the request, exactly on WebServiceTemplate.sendSourceAndReceiveToResult, there is a Transformer that removes the namespace of the NFe tag, as follows:
<?xml version="1.0" encoding="UTF-8"?>
<nfeDadosMsg xmlns="http://www.portalfiscal.inf.br/nfe/wsdl/NFeAutorizacao4">
<enviNFe xmlns="http://www.portalfiscal.inf.br/nfe" versao="4.00">
<idLote>203364605034338</idLote>
<indSinc>0</indSinc>
<NFe>
...
...
The Webservice doesn't accept the request n this case.
What should I do to avoid this behavior?

Related

merge multiple files with JAXB

It's the first time I'm using stackoverflow and I don't speak English perfectly so be nice please.
I'm using Jaxb in append mode like that
for (Document330 document : documents){
JAXBContext jContext = JAXBContext.newInstance(Document330Xml.class);
Marshaller m = jContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(document, fos);
}
And I have an output file like that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENT>
<MAILING>
<REF>M584</REF>
<LIBELLE>Mail Test 1</LIBELLE>
</MAILING>
</DOCUMENT>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENT>
<MAILING>
<REF>M585</REF>
<LIBELLE>Mail Test 2</LIBELLE>
</MAILING>
</DOCUMENT>
but I want something like that :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DOCUMENTS>
<DOCUMENT>
<MAILING>
<REF>M584</REF>
<LIBELLE>Mail Test 1</LIBELLE>
</MAILING>
</DOCUMENT>
<DOCUMENT>
<MAILING>
<REF>M585</REF>
<LIBELLE>Mail Test 2</LIBELLE>
</MAILING>
</DOCUMENT>
</DOCUMENTS>
But it is possible that I have many XML. So I do not think the Unmarshaller is the best solution
Thanks for reading me
If I remind correctly, you need to create a Documents330Xml class, which can be marshalled (you can have a look at your Document330Xml class for reference). This class needs a list of Document330Xml as field.
If you then marshall the Documents330Xml class, you should get the desired XML.

JAXB not parsing default namespace correctly

I'm writing some code to parse GPX files. These are produced by a number of different sources, from route planning websites to satnav recordings.
I'm having trouble with JAXB. It seems that it won't load the file if it uses the default name space.
For example when I try to parse the following file only the root gpx element is loaded and the child meta is not loaded and java leaves the field blank.
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<time>2015-01-24T18:13:23Z</time>
<bounds minlat="52.058110" minlon="-2.218540" maxlat="52.347110" maxlon="-1.943840" />
</metadata>
<!-- snip -->
</gpx>
However the following file loads just fine. The only difference being that the default namespace has been changed to a named one (ns1).
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ns1:gpx xmlns:ns1="http://www.topografix.com/GPX/1/1" version="1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
<metadata>
<time>2015-01-24T18:13:23Z</time>
<bounds minlat="52.058110" minlon="-2.218540" maxlat="52.347110" maxlon="-1.943840" />
</metadata>
<!-- snip -->
</ns1:gpx>
My package-info.java is currently defined with:
/**
* Pojos Representing the GPX xml.
*/
#XmlSchema(
elementFormDefault = XmlNsForm.UNQUALIFIED,
namespace = "http://www.topografix.com/GPX/1/1",
location = "http://www.topografix.com/GPX/1/1/gpx.xsd",
xmlns= {
#XmlNs(namespaceURI = "http://www.topografix.com/GPX/1/1", prefix = "gpx"),
}
)
package couling.gpxTools.pojo;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Is this something anyone else has seen before and are there any suggestions of how to get round it?
In your second XML all the elements should have the ns1 prefix to be equivalent to the first XML document.
To load the first document you need to specify elementFormDefault=XmlNsForm.QUALIFIED on the #XmlSchema annotation. Then all mapped elements within this package will default to having this namespace. Since you specified UNQUALIFIED only top level elements get the namespace qualification.

Adding Custom META tag to XML file

I created a java application that generates a whole bunch of simple XML files using the DocumentBuilder class. But now I need to go back and modify it so that all of the XML files also include this meta tag inserted before the root 'resultset' element:
<meta name="ZOOMPAGEBOOST" content="5">
So that the resulting files will look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="..\..\..\stylesheet\mysql-result.xsl"?>
<meta name="ZOOMPAGEBOOST" content="5">
<resultset>
<row>
...
</row>
</resultset>
Is this possible using the DocumentBuilder class? I figured this would have been a simple change, but I am having a difficult time finding the solution.
What you want to build is not well formed XML, DocumentBuilder won't let you build it because of that reason. Not to mention that the meta element you use in your example isn't valid XML either because it isn't closed; you have multiple problems here.
You have to have a single root element, the following is valid XML.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="..\..\..\stylesheet\mysql-result.xsl"?>
<resultset>
<meta name="ZOOMPAGEBOOST" content="5"/>
<row>
...
</row>
</resultset>

I need to analyse the xml string below

The service at this address http://www.cs12333.com:8001/Service/PersonSearchService returns the following XML:
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:findPersonListResponse xmlns:ns2="http://service.person.search/">
<return>
<result>true</result>
<resultList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:grxxBean">
<aab001>cooperation</aab001>
<aac001>34305742</aac001>
<aac002>430423198906237024</aac002>
<aac003>licong</aac003>
<aac004>2</aac004>
<aac031>2</aac031>`
<pwd>1</pwd>
</resultList>
</return>
</ns2:findPersonListResponse>
</S:Body>
</S:Envelope>
I'm reading the values like this:
dom = DocumentHelper.parseText(result);
Element root=dom.getRootElement();
The root's value is "org.dom4j.tree.DefaultElement#db23f1 [Element: <S:Envelope schemas.xmlsoap.org/soap/envelope/attributes: []/>]".
How do I get the value of tag <aac003>?
Try this expath to get <aac003> value
/*:Envelope/*:Body/*:findPersonListResponse/*:return/*:resultList/*:aac003

how to parse xml using soap web service

I have a device which periodically sends xml file to a specific webservice. Also I have a web service. My web service read this xml.
My Web service definition is :
#Name("myWebService")
#SOAPBinding(style = Style.RPC)
#WebService(targetNamespace = "http://www.abc.com.tr/")
public class MyWebService {
#Resource
WebServiceContext wsContext;
#WebMethod()
public String DeviceStatus(#WebParam(name = "SP") SP SP) {..}
My class structure is SP.java, MODULES.java, MODULE.java, SENSORS.java, SENSOR.java
Before there was no problem but when the incoming xml's namespace definition has changed my webservice started to give error like : can not find child element SP
before the incoming xml was :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:abc="http://www.abc.com.tr/">
<soap:Body>
<abc:DeviceStatus>
<SP>
<VERSION>1.2</VERSION>
<MODULES>
<MODULE><NAME>name</NAME><TYPE>1</TYPE>
<SENSORS>
<SENSOR><ID>0</ID><NAME>Input</NAME></SENSOR>
</SENSORS>
</MODULE>
</MODULES>
</SP>
</abc:DeviceStatus></soap:Body></soap:Envelope>
now the incoming xml is :
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<DeviceStatus xmlns="http://www.abc.com.tr/">
<SP>.......
I am looking forward your replies. thanks...

Categories

Resources