XML attributes order is changing after DOM processing using Java code - java

I am trying to read an XML and writing into another XML by changing some attribute values When processing XML by means of standard DOM, attribute order is changing.
Here I am trying to change the 'id' attribute value of 'staff' element.
input xml:
<company xyz="xyz" def="def">
<staff id="1" def="def" xyz="xyz" abc="abc">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
output xml:
<company def="def" xyz="xyz">
<staff abc="abc" def="def" id="2" xyz="xyz">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Expecting output xml:
<company xyz="xyz" def="def">
<staff id="2" def="def" xyz="xyz" abc="abc">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Could someone please help with the source code.
Waiting for the responses.

Related

Set multiple namespace with different prefix using jdom

I would like to have this format in xml
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:001.003.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:tech:xsd:pain.001.001.03">
<staff id="1">
<firstname>yong</firstname>
</staff>
</Document>
I am using the following code.
Namespace namespace = Namespace.getNamespace("urn:001.003.03");
Namespace namespace1 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Namespace namespace2 = Namespace.getNamespace("schemaLocation", "urn:tech:xsd:pain.001.001.03");
Element document = new Element("Document", namespace);
Document doc = new Document(document);
doc.setRootElement(document);
document.addNamespaceDeclaration(namespace1);
document.addNamespaceDeclaration(namespace2);
Element staff = new Element("staff", namespace);
staff.setAttribute(new Attribute("id", "1"));
staff.addContent(new Element("firstname",namespace).setText("yong"));
doc.getRootElement().addContent(staff);
I am getting output is:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:001.003.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="urn:tech:xsd:pain.001.001.03">
<staff id="1">
<firstname>yong</firstname>
</staff>
</Document>
I need namespace like xsi:schemaLocation but its generating namespace like xmlns:schemaLocation

Override namespace with empty namespace CXF (JBoss EAP 6.2.4.GA)

I have the following issue with CXF version 2.7.7-redhat-1, that seems like a bug in the library.
With the following request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<authorizePayment xmlns="http://ejb.example.org">
<request xmlns="">
<role xmlns:q1="http://to.homepay.example.org" xsi:type="q1:CustomerTO">
<user>7C742</user>
</role>
<error>
<errorCode>1</errorCode>
<errorMessage>1</errorMessage>
</error>
<status>
<code>1</code>
</status>
<ordAccount>1</ordAccount>
<logId>374216011815223570893830967949</logId>
<signMethod>1</signMethod>
<lang>1</lang>
<terminalId>1</terminalId>
<terminalLocation>Earth</terminalLocation>
</request>
</authorizePayment>
</soap:Body>
</soap:Envelope>
I get the following error:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Client</faultcode>
<faultstring>Unmarshalling Error: unexpected element (uri:"http://ejb.example.org", local:"request"). Expected elements are <{}request></faultstring>
</soap:Fault>
</soap:Body>
</soap:Envelope>
From the request you can clearly see that the namespace of the request is "" overridden from its parent.
If I change the request to:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<aa:authorizePayment xmlns:aa="http://ejb.example.org">
<request xmlns="">
<role xmlns:q1="http://to.homepay.example.org" xsi:type="q1:CustomerTO">
<user>7C742</user>
</role>
<error>
<errorCode>1</errorCode>
<errorMessage>1</errorMessage>
</error>
<status>
<code>1</code>
</status>
<ordAccount>1</ordAccount>
<logId>374216011815223570893830967949</logId>
<signMethod>1</signMethod>
<lang>1</lang>
<terminalId>1</terminalId>
<terminalLocation>Earth</terminalLocation>
</request>
</aa:authorizePayment>
</soap:Body>
</soap:Envelope>
The call works. I need a way to fix this issue server side, the client can't be modified to change the request.
Versions:
JBoss AS release: 7.3.4.Final-redhat-1 "Janus"
JBoss AS product: EAP 6.2.4.GA
java.version: 1.7.0_91
Set the elementFormDefault=qualified in the wsdl, generate the artifacts and redeploy the application.
Qualified means that local element declarations (xs:element within xs:complexType) refers to elements in the target namespace of the schema. Without it, they refer to elements in no namespace.

Parse XML files Java with DOM

I am currently developing a system that uses the following format of XML files to for configuration purposes:
<?xml version="1.0" encoding="utf-8"?>
<Branch>
<Department>
<Door id="1" enabled="true"/>
<Door id="2" enabled="true"/>
</Department>
<Department>
<Door id="3" enabled="true"/>
<Door id="4" enabled="false"/>
</Department>
<Department>
....
</Branch>
Do you know how could I parse this XML file?
I have been looking for other answers but I could only get it to work till the Department node. It isn't interpreting the Door node level.

JAXWS SOAP Response Schema

I am in the process of moving an older web service that had dependency on Weblogic 8 (servicegen) to a JAX WS implementation using annotations. I used the existing WSDL and JAXB to generate the data objects (marshalling/unmarshalling). However, I notice a difference in the SOAP response that is now being generated with JAXWS. I need to generate the same response, as there are over 900 clients, and can’t ask them to modify their code to use the new response schema.
The existing response looks like this:
<env:Envelope xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header/>
<env:Body>
<m:findAllCertInfoByOwnerResponse xmlns:m="http://uum.webservices.com">
<n1:result xsi:type="n2:ArrayOfWSCertInfo" xmlns:n1="http://uum.webservices.com" xmlns:n2="java:com.service.data">
<n2:WSCertInfo>
<n2:CPCreateDate> </n2:CPCreateDate>
<n2:CPExpireDate> </n2:CPExpireDate>
<n2:CPStatus> </n2:CPStatus>
<n2:PrevUniqueId>123456</n2:PrevUniqueId>
<n2:UniqueId></n2:UniqueId>
<n2:certID xsi:nil="true"/>
<n2:certStatus/>
<n2:endDate xsi:nil="true"/>
<n2:issuerDN> </n2:issuerDN>
<n2:owner></n2:owner>
<n2:profileName> </n2:profileName>
<n2:renewalStatus xsi:nil="true"/>
<n2:revokeReason/>
<n2:serialNo xsi:nil="true"/>
<n2:startDate xsi:nil="true"/>
<n2:subjectDN xsi:nil="true"/>
<n2:validCertificate>false</n2:validCertificate>
<n2:validEnrollment>true</n2:validEnrollment>
</n2:WSCertInfo>
</n1:result>
</m:findAllCertInfoByOwnerResponse>
</env:Body>
</env:Envelope>
The new JAX WS SOAP response has the following format:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:findLatestCertInfoByOwnerResponse xmlns:ns3="java:com.service.data" xmlns:ns2="http://uum.webservices.com">
<result>
<ns2:result>
<ns3:WSCertInfo>
<ns3:CPCreateDate xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:CPExpireDate xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:CPStatus xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:PrevUniqueId>123456</ns3:PrevUniqueId>
<ns3:UniqueId xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:certID xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:certStatus xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:endDate xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:issuerDN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:owner xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:profileName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:renewalStatus xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:revokeReason xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:serialNo xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:startDate xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:subjectDN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<ns3:validCertificate>false</ns3:validCertificate>
<ns3:validEnrollment>false</ns3:validEnrollment>
</ns3:WSCertInfo>
</ns2:result>
</result>
</ns2:findLatestCertInfoByOwnerResponse>
</S:Body>
</S:Envelope>
The JAXWS SOAP response is pretty close except for the following:
JAXWS response is missing the xsi:type and xmlns info. Plus it is nested in the attribute.
How can I add the xsi:type and xmlns to the attribute?
I am including the example class to show the defined web annotations.
Thanks in advance for your assistance!!!!!
The Web Service class looks like the following:
#WebService(
name = "UUMCertLocatorService",
serviceName = "UUMCertLocatorService",
portName = "UUMCertLocatorServicePort",
targetNamespace = "http://uum.webservices.com",
wsdlLocation = "UUMCertLocatorService.wsdl")
#SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED,
style = SOAPBinding.Style.DOCUMENT,
use = SOAPBinding.Use.LITERAL)
public class UUMCertLocatorServiceEndPoint {
#WebMethod(action="",operationName="findLatestCertInfoByOwner")
#WebResult(name="findLatestCertInfoByOwnerResponse",header=true,targetNamespace="java:com.service.data",partName="http://uum.webservices.com")
public FindAllCertInfoByOwnerResponse findAllCertInfoByOwner(
#WebParam(targetNamespace="http://uum.webservices.com",
partName="http://uum.webservices.com",
name="string")String uniqueID,
#WebParam(targetNamespace="http://uum.webservices.com",
partName="http://uum.webservices.com",
name="string0")String adminSessionSpec,
#WebParam(targetNamespace="http://uum.webservices.com",
partName="http://uum.webservices.com",
name="string1")String webKey) throws WSUUMException
{
LogHelper.debug(UUMCertLocatorServiceEndPoint.class, "Params: {} {} {} ", uniqueID,adminSessionSpec,webKey);
final ObjectFactory objectFactory = new ObjectFactory();
FindAllCertInfoByOwnerResponse response = objectFactory.createFindAllCertInfoByOwnerResponse();
WSCertInfo certinfo = objectFactory.createWSCertInfo();
certinfo.setBofaPrevUniqueId("123456");
ArrayOfWSCertInfo arraycert = objectFactory.createArrayOfWSCertInfo();
arraycert.getWSCertInfo().add(certinfo);
response.setResult(arraycert);
return response;
}
}

How can i parse the following XML using JDOM

I have an XML document as follows:
<?xml version="1.0" encoding="UTF-8"?>
<decision>
<question id="0">
<questionText>What type is your OS?</questionText>
<answer id="0">
<answerText>windows</answerText>
</answer>
<answer id="1">
<answerText>linux</answerText>
</answer>
<answer id="2">
<answerText>mac</answerText>
</answer>
</question>
<question id="1">
<questionText>What are you looking for?</questionText>
<answer id="0">
<answerText>table</answerText>
<question id="0">
<questionText>Which color table you want?</questionText>
<answer id="0">
<answerText>green</answerText>
</answer>
<answer id="1">
<answerText>black</answerText>
</answer>
<answer id="2">
<answerText>pink</answerText>
</answer>
</question>
</answer>
<answer id="1">
<answerText>chair</answerText>
</answer>
<answer id="2">
<answerText>bed</answerText>
</answer>
<answer id="3">
<answerText>cloth</answerText>
</answer>
</question>
Now I want to parse the above XML using jdom in Java. It kind of recursive and important thing to note is a Question can't be a direct child of Question and same applies for Answer.
Article
In the light of previous related questions, I'd like to repeat and stress the advice of others (like JB Nizet commented on this question):
Learn Java, learn XML, pick the tools and API's you need for your project and learn to use those too. If at one point you get into trouble, everybody here will be happy to help you out debugging your code.
I'm aware that this may seem harsh but it gets to the point where your program gets built by StackOverflow users and not yourself.
That being said, the link at the top of this answer leads to a tutorial on using JDOM to traverse your XML.
Use Element.getChildren(String) to get all of the question tags and loop through that List - calling getChildren(String) to get all of the answers, or getChild(String) if there can be only one child element.
first that you need use is XSD to validate the XML.

Categories