Reading XML within XML using Java - java

How to read From XML <returnMsg>Successful</returnMsg> value of this tag in java?
How to read the data of the tags in this example.
I am getting The processing instruction target matching "[xX][mM][lL]" is not allowed. Exception is getting
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<doServiceResponse xmlns="http://ocs.ztesoft.com">
<doServiceReturn><?xml version="1.0" encoding="UTF-8"?>
<zsmart>
<Data>
<header>
<returnMsg>Successful</returnMsg>
<ACTION_ID>ModifyBalReturnAllBal</ACTION_ID>
<REQUEST_ID>0032013070900000503</REQUEST_ID>
<returnCode>0</returnCode>
</header>
<body>
<TransactionSN>503</TransactionSN>
</body>
</Data>
</zsmart>
</doServiceReturn></doServiceResponse></soapenv:Body></soapenv:Envelope>
JAVA CODE
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
is = new InputSource();
is.setCharacterStream(new StringReader(respString));
doc = db.parse(is);
nodes = doc.getElementsByTagName("soapenv:Envelope");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList txnStatus = element.getElementsByTagName("returnCode");
Element line = (Element) txnStatus.item(0);
bean.setTxnStatus(getCharacterDataFromElement(line));
NodeList message = element.getElementsByTagName("returnMsg");
line = (Element) message.item(0);
bean.setMessage(getCharacterDataFromElement(line));
}
Exception
org.xml.sax.SAXParseException: The processing instruction target matching "[xX][mM][lL]" is not allowed.

There are many way to convert XML file to JAVA OBJECT.
SAX and JAXB algorithms are two of them.
JAXB algorithm is more easily to use. i prefer to use JAXB.
HERE is the link that helps you to create Object from XML file.
enjoy it...
http://www.mkyong.com/java/jaxb-hello-world-example/

Related

Get element value from XML with XPath

I have XML file like this:
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents- xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBL-Invoice-2.1.xsd">
<cac:AccountingSupplierParty>
<cac:Party>
<cac:PartyIdentification>
<cbc:ID schemeID="schema1">123231123</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="schema2">2323232323</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="schema3">4442424</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:CityName>İstanbul</cbc:CityName>
<cac:Country>
<cbc:Name>Turkey</cbc:Name>
</cac:Country>
</cac:PostalAddress>
</cac:Party>
</cac:AccountingSupplierParty>
</Invoice>
I want to access schemeID="schema=2" value. I try XPath and document.getElementsByTagName. I can access elements with document.getElementsByTagName, since is multiple I can't access the element I want. When I try to with XPath, I can't access any elements from XML.
Here is my XPath implementation:
try {
String decoded = new
String(DatatypeConverter.parseBase64Binary(binaryXmlData));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(decoded));
Document doc = db.parse(is);
String expression = "/Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyIdentification/cbc:ID#[schemaID='schema2']/text()";
String schema2 = (String) xPath.compile(expression).evaluate(doc, XPathConstants.STRING);
System.out.println(schema2);
//schema2 is null
//Above this code block returns correct value
NodeList nl = doc.getElementsByTagName("cbc:CityName");
System.out.println(nl.item(0).getTextContent());
} catch () {
}
binaryXmlData is source of my XML. First, I convert base64binary data to xml. Am I doing to convertion wrong or my xpath implementation is wrong ?
There are many problems with your code and your XML, including:
Your XML is not well-formed. The closing quote of the cbc
namespace prefix is missing.
Your Java code never defines a NamespaceContext.
See also How does XPath deal with XML namespaces?

Get SOAP element by tag with namespace in Java

I wondered how it was possible to get the content of a SOAP response body child when the child tag looks like this:
<sth:x>...</sth:x>
In the Oracle docs I found how to loop all Elements with a specific name.
But therefore I need to create an Element first that specifies the name of the tag I want to search.
How do I create an element looking like the one above? I know how to make one like this:
<sth:id sth="asdf">
But that doesn't really work.
Here is the server-response I attempt to read.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<ns5:loginResponse xmlns:ns5="x">
<ns5:id>Value I am looking for</ns5:id>
<ns5:rc>0</ns5:rc>
</ns5:loginResponse>
</soapenv:Body>
</soapenv:Envelope>
Thanks for your help :)
Try this:
String xml = "YourXMl";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS("x","id");
System.err.println(nodes.item(0).getChildNodes().item(0).getNodeValue());
There are two important things factory.setNamespaceAware(true); - to turn on support for xml namespaces.
doc.getDocumentElement().getElementsByTagNameNS("x","id"); To get element using namespace uri and element name. And here is <ns5:loginResponse xmlns:ns5="x"> declaration of namespace uri. x is uri ns5 is namespace.
I found the answer:
The Element in question was within another SOAPBodyElement.
So looping through both elements gave me the correct Value:
body = reply.getSOAPBody();
Iterator replyIt = body.getChildElements();
while(replyIt.hasNext()){
SOAPBodyElement replysbe = (SOAPBodyElement) replyIt.next();
Iterator replyIt2 = replysbe.getChildElements();
SOAPElement sentSE = (SOAPElement) replyIt2.next();
sessionID = sentSE.getValue();
}

Xml Output generation from excel sheet

I am trying to convert an excel file into xml file in java.
try {
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();
Element root = doc.createElement("dataroot");
doc.appendChild(root);
Element Details = doc.createElement("DATA");
root.appendChild(Details);
for(int i=0; i<list.size()-2; i +=3 ) {
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode(String.valueOf(list.get(i))));
Details.appendChild(name);
Element id = doc.createElement("Empid");
id.appendChild(doc.createTextNode(String.valueOf(list.get(i+1))));
Details.appendChild(id);
Element ad = doc.createElement("Add");
ad.appendChild(doc.createTextNode(String.valueOf(list.get(i+2))));
Details.appendChild(ad);
Element mo = doc.createElement("Mobile");
mo.appendChild(doc.createTextNode(String.valueOf(list.get(i+3))));
Details.appendChild(mo);
}
// Save the document to the disk file
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
// format the XML nicely
aTransformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
aTransformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
This is my code for reading an ArrayList of excel data (with values and null) and convert it into xml.
my output is:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<dataroot generated="2015-04-26T10:52:27">
<DATA>
<Name>Rony</Name>
<Empid>FBL123</Empid>
<Add>Dhaka</Add>
<Mobile>12333333</Mobile>
</DATA>
<DATA>
<Name>12333333</Name>
<Empid>Azam</Empid>
<Add>FBL321</Add>
<Mobile>Dhaka</Mobile>
</DATA>
<DATA>
<Name>Dhaka</Name>
<Empid>67778888</Empid>
<Add>Rony</Add>
<Mobile>Chandpur</Mobile>
</DATA>
<DATA>
<Name>Chandpur</Name>
<Empid>099776655</Empid>
<Add>Azam</Add>
<Mobile>9988</Mobile>
</DATA>
</dataroot>
But my desired xml output is:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2015-04-26T10:52:27">
<DATA>
<Name>Rony</Name>
<Empid>FBL123</Empid>
<Add>Dhaka</Add>
<Mobile><Prop ID="Personal" ValStr="Mrs. YYYYYYYY " /><Prop ID="FAMILY" ValStr="Mrs. ZZZZZZZZ" />"</CLIENTPROP>
</DATA>
<DATA>
<Name>AZAM</Name>
<Empid>FBL321</Empid>
<Add>Dhaka</Add>
<Mobile><Prop ID="Personal" ValStr="Mrs. YYYYYYYY " /><Prop ID="FAMILY" ValStr="Mrs. ZZZZZZZZ" />"</CLIENTPROP>
</DATA>
</dataroot>
How i Can do it?
To have "DATA" tags, you shoud move the lines below into your for statement:
Element Details = doc.createElement("DATA");
root.appendChild(Details);
To add some property on "dataroot", use the code below:
root.setAttribute("generated", "2015-04-26T10:52:27");
On Mobile element, try this:
Element mo = doc.createElement("Mobile");
Element prop = doc.createElement("Prop");
prop.setAttribute("ID", "FAMILY");
prop.setAttribute("ValStr", "Mrs. YYYYYYYY");
Element prop2 = doc.createElement("Prop");
prop2.setAttribute("ID", "Personal");
prop2.setAttribute("ValStr", "Mrs. ZZZZZZZZ");
mo.appendChild(prop);
mo.appendChild(prop2);
Try to read about DOM parser on this tutorial as well. How to create XML file in Java – (DOM Parser)

XML Namespace - xmlns

I'm trying to generate a XML from Java.
Something like this:
<?xml version="1.0" encoding="UTF-8"?>
<ElementFile
xmlns="http://www.url.com/bla/bla"
xmlns:common="http://www.url.com/bla/bla/bla">
<Regulation>
<blablaElement>0000-0000</blablaElement>
</Regulation>
</ElementFile>
To do this, I wrote the following code:
ElementFile = document.addElement( "ElementFile" )
.addNamespace("xmlns","http://www.url.com/bla/bla")
.addNamespace("common", "http://www.url.com/bla/bla/bla");
But the generated code is:
<?xml version="1.0" encoding="UTF-8"?>
<ElementFile
xmlns:xmlns="http://www.url.com/bla/bla"
xmlns:common="http://www.url.com/bla/bla/bla">
<Regulation>
<blablaElement>0000-0000</blablaElement>
</Regulation>
</ElementFile>
However, if I write this (without xmlns)
ElementFile = document.addElement( "ElementFile" )
.addNamespace("","http://www.url.com/bla/bla")
.addNamespace("common", "http://www.url.com/bla/bla/bla");
Then, the xml generates the line properly but it adds xmlns=" " by default:
<?xml version="1.0" encoding="UTF-8"?>
<ElementFile
xmlns="http://www.url.com/bla/bla"
xmlns:common="http://www.url.com/bla/bla/bla">
<Regulation xmlns="">
<blablaElement>0000-0000</blablaElement>
</Regulation>
</ElementFile>
I'm quite lost...
[UPDATED QUESTION]
I'm sorry, but I don't understand how to integrated your answer in my code. I am new to this domain.
My complete code is:
XMLWriter writer = new XMLWriter(new FileWriter("xmlFileName"), format);
Document document = DocumentHelper.createDocument();
Element ElementFile = document.addElement( "ElementFile" )
.addNamespace("xmlns","http://www.url.com/bla/bla")
.addNamespace("xmlns:common", "http://www.url.com/bla/bla/bla");
Element Regulation = ElementFile.addElement( "Regulation" );
Element blablaElement = Regulation.addElement( "blablaElement" )
.addText( "0000-0000" );
writer.write(document);
writer.close();
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
dbfac.setNamespaceAware(true);
DocumentBuilder docBuilder;
docBuilder = dbfac.newDocumentBuilder();
DOMImplementation domImpl = docBuilder.getDOMImplementation();
Document doc = domImpl.createDocument("http://www.url.com/bla/bla", "ElementFile ", null);
doc.setXmlVersion("1.0");
doc.setXmlStandalone(true);
Element elementFile = doc.getDocumentElement();
elementFile.setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:common","http://www.url.com/bla/bla/bla");
and add elements to this doc
you have a similar question here: How to create attribute (xmlns:xsd) to XML node in Java?

Correct namespace declaration in Java

I need to create an XML document with the following structure:
<?xml version="1.0" ?>
<Cancelacion xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
RfcEmisor="VSI850514HX4"
Fecha="2011-11-23T17:25:06"
xmlns="http://cancelacfd.sat.gob.mx">
<Folios>
<UUID>BD6CA3B1-E565-4985-88A9-694A6DD48448</UUID>
</Folios>
</Cancelacion>
The structure MUST be that way. But I'm not very familiar with the namespace declaration for the XML Elements. I can correctly generate an XML with the following structure:
<?xml version="1.0" ?>
<Cancelacion RfcEmisor="VSI850514HX4"
Fecha="2011-11-23T17:25:06"
xmlns="http://cancelacfd.sat.gob.mx">
<Folios>
<UUID>BD6CA3B1-E565-4985-88A9-694A6DD48448</UUID>
</Folios>
</Cancelacion>
But the problem is that I can't get to include the xmls:xsd and xmlns:xsi correctly. The code for properly generating the previously mentioned code is:
// Crear un document XML vacío
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
dbfac.setNamespaceAware(true);
DocumentBuilder docBuilder;
docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
doc.setXmlVersion("1.0");
doc.setXmlStandalone(true);
Element cancelacion = doc.createElementNS("http://cancelacfd.sat.gob.mx","Cancelacion");
cancelacion.setAttribute("RfcEmisor", rfc);
cancelacion.setAttribute("Fecha", fecha);
doc.appendChild(cancelacion);
Element folios = doc.createElementNS("http://cancelacfd.sat.gob.mx", "Folios");
cancelacion.appendChild(folios);
for (int i=0; i<uuid.length; i++) {
Element u = doc.createElementNS("http://cancelacfd.sat.gob.mx","UUID");
u.setTextContent(uuid[i]);
folios.appendChild(u);
}
You can add additional namespaces by calling setAttributeNS method on the root element
as shown in below example
// get the root element
Element rootElement = xmlDoc.getDocumentElement();
// add additional namespace to the root element
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
You can then add elements to a given namespace by using the method like below
// append author element to the document element
Element author = xmlDoc.createElement("xsd:element");
Read this article for more details.

Categories

Resources