Read specific data from XML using Java (DOM) - java

I want to extrat some data from an invoice (XML) using Java. I'm using the example on this link (https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/)
The problem is my XML doesn't look like the one in the example, here is mine:
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:bfa2="http://www.buzonfiscal.com/ns/addenda/bf/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" NumCtaPago="4007" LugarExpedicion="CUAJIMALPA DE MORELOS,CIUDAD DE MEXICO" metodoDePago="99" tipoDeComprobante="ingreso" total="2490" subTotal="2146.55" fecha="2016-12-14T10:23:08" folio="57318" serie="BT" version="3.2" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd http://www.buzonfiscal.com/ns/addenda/bf/2 http://www.buzonfiscal.com/schema/xsd/Addenda_BF_v20.xsd">
<cfdi:Emisor nombre="BEST BUY STORES S. DE R.L. DE C.V." rfc="BBS070606D33">
<cfdi:DomicilioFiscal codigoPostal="05348" pais="MEXICO" estado="CIUDAD DE MEXICO" municipio="CUAJIMALPA DE MORELOS" referencia="EDIFICIO CENTURY PLAZA" colonia="SANTA FE CUAJIMALPA" noInterior="PISO 2 OFNA 202 Y 203 P3 Y P4" noExterior="440" calle="AV. SANTA FE" />
<cfdi:ExpedidoEn codigoPostal="05348" pais="MEXICO" estado="CIUDAD DE MEXICO" municipio="CUAJIMALPA DE MORELOS" colonia="SANTA FE" calle="AVENIDA SANTA FE NO. 440 PISO 2 (OFICINA 203 Y 204),PISO 3 Y PISO 4" />
<cfdi:RegimenFiscal Regimen="Regimen General de Ley Personas Morales" />
</cfdi:Emisor>
<cfdi:Receptor nombre="VIALCOMA S.A DE C.V" rfc="VIA8011248W0">
<cfdi:Domicilio codigoPostal="54080" pais="México" estado="Estado de México" municipio="Tlalnepantla de Baz" referencia="VIALCOMA S.A. de C.V." localidad="Tlalnepantla de Baz" colonia="EL MIRADOR" noExterior="107" calle="AV. GUSTAVO BAZ " />
</cfdi:Receptor>
<cfdi:Conceptos>
<cfdi:Concepto importe="2146.55" valorUnitario="2146.55" descripcion="PANASONIC PHONE KX-PRD260MEB" noIdentificacion="000000001000189023" unidad="PZA" cantidad="1" />
</cfdi:Conceptos>
<cfdi:Impuestos totalImpuestosTrasladados="343.45">
<cfdi:Traslados>
<cfdi:Traslado importe="343.45" tasa="16.0" impuesto="IVA" />
</cfdi:Traslados>
</cfdi:Impuestos>
<cfdi:Complemento>
<tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigital.xsd" UUID="2e36935c-4948-4f7b-9b29-477546aa2922" version="1.0" />
</cfdi:Complemento>
<cfdi:Addenda>
<bfa2:AddendaBuzonFiscal version="2.0">
<bfa2:Receptor email="" />
<bfa2:TipoDocumento descripcion="Factura" nombreCorto="FAC" />
<bfa2:CFD totalConLetra="Dos mil cuatrocientos noventa pesos 00/100 M.N." fechaEnvio="2016-12-12T10:42:15-06:00" />
<bfa2:Extra valor="02028001014520161212" atributo="numTicket" />
</bfa2:AddendaBuzonFiscal>
</cfdi:Addenda>
</cfdi:Comprobante>
I want to get (print on console) the data on "folio" "serie" and "UUID"
Can someone help me please?

The following should give your desire output.This example is based on DOM parser as you have provide.
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("cfdi:Comprobante");
org.w3c.dom.Node nNode = nList.item(0);
System.out.println("folio: " + ((Element) nNode).getAttribute("folio"));
System.out.println("serie: " + ((Element) nNode).getAttribute("serie"));
NodeList list = doc.getElementsByTagName("cfdi:Complemento");
for (int i = 0; i < 1; i++) {
NodeList child = list.item(i).getChildNodes();
for (int j = 0; j < child.getLength(); j++) {
if (child.item(j).getNodeName().equals("tfd:TimbreFiscalDigital")) {
String uuid = ((Element)(child.item(j))).getAttribute("UUID");
System.out.println("UUID : " + uuid);
}
}
}
console output:
Root element :cfdi:Comprobante
----------------------------
folio: 57318
serie: BT
UUID : 2e36935c-4948-4f7b-9b29-477546aa2922

Related

Remove certain recurring elements in xml

I have a xml and want to remove certain elements from the xml document. Initially I wanted to remove the control characters coming in these elements or completely remove the data in these elements.
My sample payload is as below. I wanted to remove resume/content and comments.
<Jobs>
<candidates>
<address2/>
<application>
<comments><BR />Test emp1711 Newberg St. #4 Oregon, CA 97229 444-123-9752 testcandidate#gmail.com<BR />authorName: Test 1
comment content: <b>To: test test (test.test#yahoo.com)</b><br /><br />From: abc Recruitment Team (test#abc.com)<br /><br />Subject: Tester at abc<br /><br /><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="12" COLOR="#353A3F" LETTERSPACING="0" KERNING="0">Hello test,</FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="12" COLOR="#353A3F" LETTERSPACING="0" KERNING="0"></FONT></P></TEXTFORMAT><TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Verdana" SIZE="12" COLOR="#353A3F" LETTERSPACING="0" KERNING="0">Thank you for your interest in our abc opening. </FONT></P></comments>
<disposition>tesr Skills match</disposition>
<eId>xyz</eId>
<gender>Male</gender>
<lastUpdatedDate>1340687163</lastUpdatedDate>
<race>Undefined</race>
<resume>
<content>Test<BR />Human Resources Manager / Business Partner <BR /> portland, Oregon; 4454; <BR /> Phone 020 444456;Mobile 088768999;E-mail test#yahoo.com <BR /></content>
<format>Text</format>
</resume>
<sentDate>1789993473</sentDate>
<source>Linkedin</source>
<sourceType>Import</sourceType>
<veteranStatus>Undefined</veteranStatus>
<workflowState>Offer Accepted</workflowState>
<city>portland</city>
<companyName/>
<country>US</country>
<eId>xyz</eId>
</application>
</candidates>
<candidates>
<address2/>
<application>
<comments><BR />Test emp2711 Newberg St. #4 Oregon, CA 97229 444-123-9752 testcandidate2#gmail.com<BR />authorName: Test 2
</comments>
<disposition>Skills match</disposition>
<eId>xyz</eId>
<gender>female</gender>
<lastUpdatedDate>1340687163</lastUpdatedDate>
<race>Undefined</race>
<resume>
<content>Test<BR />Program Manager / Business Partner <BR /> portland, Oregon; 4454; <BR /> Phone 020 444456;Mobile 088768999;E-mail test#yahoo.com <BR /></content>
<format>Text</format>
</resume>
<sentDate>178444473</sentDate>
<source>Linkedin</source>
<sourceType>Import</sourceType>
<veteranStatus>Undefined</veteranStatus>
<workflowState>Offer Accepted</workflowState>
<city>portland</city>
<companyName/>
<country>US</country>
<eId>xyzabc</eId>`enter code here`
</application>
</candidates>
</Jobs>
Build the XML into a Document using DocumentBuilderFactory and pass it to a function like below: (nodeName would be "comments" etc.)
removeAllNodes(Document doc, String nodeName){
DocumentTraversal t = (DocumentTraversal) doc;
Node root = doc.getDocumentElement();
NodeIterator iterator =
t.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, null, true);
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
Element e = (Element) n;
if (nodeName.equals(e.getTagName()))
root.removeChild(e);
}
}

Selenium getText() method does not get correct text with diacritics

I am trying to get text from a field that contains diacritics like: ă,ă-Ă-â-Â-î-Î-ş-Ş-ţ-Ţ
My method for get text looks like this:
public void invalidEmailValidation(){
String expectedInner = LoginPage.invalidMailMessage(driver).getAttribute("innerText");
String expectedText = LoginPage.invalidMailMessage(driver).getText();
System.out.print("EXPECTED1: " + expectedInner + "\n" +
"EXPECTED2: " + expectedText );
Assert.assertEquals("Expcted Inner: Adresa de email nu este validă",
expectedInner, "Adresa de email nu este validă");
Assert.assertEquals("Expcted text: Adresa de email nu este validă",
expectedText, "Adresa de email nu este validă");
The output from the method is here:
I mention that my project have UTF-8 encoding
Also UTF-8 encoding is added in pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
Other informations:
IDE: IntellijIdea 14.1.4 (comunity version)
OS: Windows 7 (english language as default)
I already checked the following questions from StackOverflow:
Cannot verify text with diacritics
Empty string

XPAGES : take the answer of user by the dropdownbutton

I try to recover the answer of the user but i can't.
I have a dropdownbutton, I recover the list and the value by default by a java class.
It's ok about this.
But when I try to push another text of the list : nothing append...
<xe:dropDownButton id="dropdownEtatDoc">
<xe:this.treeNodes>
<xe:basicContainerNode>
<!-- Affiche l'état du document par défaut-->
<xe:this.label id="labelEtatDoc">
<![CDATA[#{javascript:
etatDoc.nomEtatDoc;
}]]>
</xe:this.label>
<!-- affiche la liste des états du document-->
<xe:repeatTreeNode var="index" value="#{etatDoc.listEtatDoc}">
<xe:this.children>
<xe:basicLeafNode label="#{index}" submitValue="#{index}" />
</xe:this.children>
</xe:repeatTreeNode>
</xe:basicContainerNode>
</xe:this.treeNodes>
<!-- actualise la chaine sélectionnée-->
<xp:eventHandler event="onItemClick" submit="true"
refreshMode="complete">
<xp:this.action>
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:
var montest = getComponent("dropdownEtatDoc").submittedValue();
etatDoc.nomEtatDoc = montest;
}]]></xp:this.script>
</xp:executeScript>
</xp:this.action>
</xp:eventHandler>
</xe:dropDownButton>
An idea ? Thanks !
I use a combobox and it's ok, I don't know why...
My code for anyone has the same problem :
<xp:this.beforePageLoad>
<xp:executeScript>
<xp:this.script><![CDATA[#{javascript:
viewScope.etat = etatDoc.listEtatDoc;
viewScope.grdFam = chapitre.listChapitre1;
}]]></xp:this.script>
</xp:executeScript>
</xp:this.beforePageLoad>
<xp:div>
<!--**************** DEBUT CBXETATDOC *************************************************************-->
<xp:label value="Etat : " id="label1"></xp:label>
<xp:comboBox id="cbxEtat">
<xp:selectItem itemLabel="Tout" itemValue="" />
<xp:selectItems value="#{viewScope.etat}" />
<xp:eventHandler event="onchange" submit="true" refreshMode="complete" immediate="true">
<xp:this.action>
<xp:executeScript>
<xp:this.script>
<![CDATA[#{javascript:
var etat = getComponent("cbxEtat").submittedValue;
}]]>
</xp:this.script>
</xp:executeScript>
</xp:this.action>
</xp:eventHandler>
</xp:comboBox>
Assuming "etatDoc" is a NotesXspDocument and not a bean.
1) If you are having trouble getting the submitted value, get it via the context:
var submitted = context.getSubmittedValue();
print("submitted: " + submitted); //check if it is there, delete afterward
switch(submitted){
case("btn1")
//doSomething(submitted);
break;
}
Since you are using the repeater node, it would not be practical to use the switch for all of it, but you could make a default where you do what ever you need to with the index value.
2) then I do not think you are setting the value properly. Try:
var montest = submitted;
etatDoc.replaceItemValue("nomEtatDoc", etatDoc.getItemValue("nomEtatDoc").add(montest))
//gets the current list (java.util.Vector) from the document
//add a value to the list
//replace the value with the new list
I am assuming that you are not using a self-made-bean for this.

Recieving json with html tags fails in PHP

I have a website that sends and receives string in JSON format from my Java REST server with jersey. Everything works fine until I'm trying to receive a json object with html tags.
A println on my java server tells me that this data has ben sent:
data sent: {"text": "Wij zijn Pixel Apps, ook wel bekend als Groep 6.<br />
Samen met onze 6 groepsleden verzorgen wij het reilen en zijlen op Ford Lommel Proving Grounds.<br />
<br />
<b>Korte inleiding</b><br />
<p>Onze taak bestaat er uit een functionele applicatie te maken binnen Windows 8. De app bestaat er uit de chauffeurs te begeleiden op hun testritten.<br />De chauffeurs worden onder andere geholpen bij het bekijken van hun routineplan, het bijhouden van notities en het overzetten van de resultaten naar het hoofdgebouw.</p>
<b>Bijkomende hoort natuurlijk het onderhouden van deze website.</b>
<p>Zoals u kan zien vind u hierboven het navigatiemenu.<br />
Voor meer informatie over ons project kan u terecht bij <i>Over ons</i><br />
Wenst u contact op te nemen? U kan zich wenden naar het tabblad <i>Contact</i><br />
Indien u meer over de individuele groepsleden wil weten kan u terecht bij <i>Leden</i><br />
Als u meer informatie wenst over ons project, gelieve contact op te nemen met ons en wij verzorgen uw verzoek.</p>
<b>Happy browsing!</b>"}
It's basically a simple json with one variable "text" and as content some HTML formatted content. I've googled my issue and it seems that this should work fine.
Here's my java GET method that fails to send json with html tags in it's content:
#GET
#Path("gettext")
#Produces("application/json")
public String getJson(#QueryParam("id") String id, #QueryParam("taalcode") String taalcode) {
Connectie c = new Connectie();
try
{
c.openConnectie();
String content = c.getCms(id, taalcode);
if (content == null || content.equals("")) {
content = "{ \"text\" : \"Geen tekst gevonden.\" }";
}
System.out.println("data send: "+content);
return content;
}
catch(Exception e)
{
System.out.println("data send: { \"text\" : \"Server error, sorry.\" }");
return "{ \"text\" : \"Server error, sorry.\" }";
}
}
My put method successfully receives a json with html tags in it's content.
Here's how I receive my json objects in PHP (which again works if no html tags are present):
public function getCMS($id) {
$taalcode = '';
if($this->session->userdata('language') == 'nederlands') {
$taalcode = 'NL';
} else {
$taalcode = 'EN';
}
$curl_instance = curl_init();
curl_setopt($curl_instance, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_instance, CURLOPT_URL, 'http://192.168.0.251:8084/Groep1/webresources/cmspost/gettext?id='.$id.'&taalcode='.$taalcode);
try {
$data = json_decode(curl_exec($curl_instance), true);
if ($data == null) {
$data['text'] = "Altough I set a string in my java get method if it's null, this message is always printed";
}
return $data;
} catch (HttpException $ex) {
$data['text'] = $ex;
return $data;
}
}
In PHP I test if ($data == null) which is always true, even though I set a string manually in my GET method if appears to be null before sending the string.
What am I doing wrong?
The problem is not with HTML. The problem is that JSON does not allow multi-line strings. If you remove the line breaks, your JSON works fine.
NB that you really should use a JSON library for building JSON, rather than doing it yourself, because it will deal with this kind of issue.

org.xml.sax.SAXParseException: Content is not allowed in prolog Problems

I'm trying to work with jasperreports and I have the next mistake when I try to run an application I'm developing:
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at es.uniway.principal.EnviarInfoPedido.infoPedido_Mayorista(EnviarInfoPedido.java:452)
at es.uniway.principal.ObtenerProcesos.ejecutar(ObtenerProcesos.java:78)
at es.uniway.principal.SincronizadorCloud.main(SincronizadorCloud.java:14)
For the code I have this I'm writing:
xtw = xof.createXMLStreamWriter(new FileWriter(nombre_report+".xml"));
//
// xmlwriter.WriteStartDocument();
xtw.writeStartDocument();
// nodo 1
xtw.writeStartElement("request");
xtw.writeAttribute("operationName", "runReport");
xtw.writeAttribute("locale", "es");
// nodo 2
xtw.writeStartElement("argument");
xtw.writeAttribute("name","RUN_OUTPUT_FORMAT");
xtw.writeCharacters(formato.trim().toUpperCase());
xtw.writeEndElement(); // nodo 2
// nodo 2
xtw.writeStartElement("argument");
xtw.writeAttribute("name","USE_DIME_ATTACHMENTS");
xtw.writeCharacters("1");
xtw.writeEndElement(); // nodo 2
// nodo 3
xtw.writeStartElement("resourceDescriptor");
xtw.writeAttribute("name","");
xtw.writeAttribute("wsType","");
xtw.writeAttribute("uriString", CARPETA_REPORT + nombre_report.trim());
xtw.writeAttribute("isNew", "false");
//// nodo 4
//xmlwriter.WriteStartElement("label", null);
//xmlwriter.WriteEndElement(); // nodo 4
if (nombre_report == "LINEAS_DETALLE_COSTE" && id_albaran_coste.trim() != " ")
{
// para las líneas detalle coste teneos 2 parámetros:
// - NALNARANC --> albaran para calcular el descuento de coste
// - NALBARANV --> albaran para calcular el descuento de venta
// parametro 1
xtw.writeStartElement("parameter");
xtw.writeAttribute("name", "NALBARANC");
xtw.writeAttribute("class", "java.lang.Integer");
xtw.writeCharacters(id_albaran_coste);
xtw.writeEndElement(); // parametro 1
// parametro 2
xtw.writeStartElement("parameter");
xtw.writeAttribute("name", "NALBARANV");
xtw.writeAttribute("class", "java.lang.Integer");
xtw.writeCharacters(id_albaran);
xtw.writeEndElement(); // parametro 1
}
else
{
// parametro 1
xtw.writeStartElement("parameter");
xtw.writeAttribute("name", "NALBARAN");
xtw.writeAttribute("class", "java.lang.Integer");
xtw.writeCharacters(id_albaran);
xtw.writeEndElement(); // parametro 1
}
xtw.writeEndDocument();
xtw.flush();
xtw.close();
What is wrong?. Thanks. The xml is this and I don't know what is wrong:
<?xml version="1.0" ?> <request operationName="runReport" locale="es"> <argument name="RUN_OUTPUT_FORMAT"> CSV </argument> <argument name="USE_DIME_ATTACHMENTS"> 1</argument><resourceDescriptor name="" wsType="" uriString="/ICLDI/CABECERA_PEDIDO_CONTRATO" isNew="false"><parameter name="NALBARAN" class="java.lang.Integer">6658</parameter></resourceDescriptor></request>
Thanks once again.
Remove the space here:
<?xml version="1.0" ?> <request
It should look like this:
<?xml version="1.0" ?><request
Other problems may be that you have some invisible extra character on the very beginning of the XML file, for example Byte-Order-Mark (also known as BOM) It shows that a UTF-16 file is high-endian or low-endian.

Categories