Parse Blockly generated XML code to Java objects - java

I've to parse Blockly generated XML code. Generally i use Xstream parser for parsing XML. But XStream parser not working since this XML has child tags same name as Parent tag.
I found this library which converts Blockly xml code to Java objects. But this library fails for Complex Blockly XML code shown below. I tried to edit library to enable parsing for the below xml code. But its not working.
<?xml version="1.0" encoding="UTF-8"?>
<block type="event_click" id="Bp64nx|xcR*L:(K(`u78" x="146" y="81">
<field name="Event_Type">none</field>
<field name="do_on_page_load">TRUE</field>
<statement name="action">
<block type="database_yes_no" id="#0OCYIX:}#uT?0[UbS=r">
<value name="Check_Name">
<block type="database_check_field" id="Sl-IEwB~/hv?9mQYYBB]">
<field name="Enum_Name">StockType</field>
<field name="Enum_Value">Serialised</field>
</block>
</value>
<statement name="Yes">
<block type="action_toggle_field" id="}X:#xcfp,!+bk7MA.sgJ">
<field name="action">show</field>
<field name="id">4930</field>
<next>
<block type="action_toggle_field" id="h)QgD#prJCVcm;BMG$VR">
<field name="action">hide</field>
<field name="id">4932</field>
<next>
<block type="action_toggle_field" id="1V{g?Cdh2ww}ihWhtovG">
<field name="action">show</field>
<field name="id">4961</field>
</block>
</next>
</block>
</next>
</block>
</statement>
<statement name="No">
<block type="action_toggle_field" id="InDTOg;B2!go_rD;WIb~">
<field name="action">hide</field>
<field name="id">4930</field>
<next>
<block type="action_toggle_field" id="f{Ae_|^jEb{CR5$?9Ku.">
<field name="action">show</field>
<field name="id">4932</field>
<next>
<block type="action_toggle_field" id="#-^o/f|Iv0WIids+VcJv">
<field name="action">hide</field>
<field name="id">4961</field>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</statement>
</block>
I don't see much help on Blockly xml parsing.
Please let me know if there is a solution or library to parse Blockly generated XML code.
Thank you.

Stream-based XML parsers require you to maintain extra state, usually a stack. Simple if (tagName == 'block') {...} is not sufficient.
For your above example, the stack might look like:
block
statement
block
statement
block
next
block
next
block
field
Each item might have a pointer to the parsed object under construction, so you can add the child to it, and know what object to continue working on after an end tag.
While it uses XmlPullParser instead of a stream parser, you may want to look at com.google.blockly.model.BlockFactory.fromXml(..) in the blockly-android repo.

Related

Index XML files in Apache Solr as plain text

Is there any way to dump all contents of xml file in a single content field??
schema.xml
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="content" type="text_general" indexed="true" stored="true" multiValued="false" termVectors="true" termPositions="true" termOffsets="true"/>
code used for indexing
HttpUrlConnection solrHttpURLConnection = "http://localhost:7892/solr/myCore/update/extract?literal.id=1234&commit=true "
solrHttpURLConnection.setDoOutput(true);
solrHttpURLConnection.setDoInput(true);
solrHttpURLConnection.setUseCaches(false);
solrHttpURLConnection.setAllowUserInteraction(false);
solrHttpURLConnection.setRequestProperty("Content-type", type);
solrHttpURLConnection.connect();
i am taking outputstream from this url and writing contents by taking input stream from dataServer.
NOTE:
the above code works for all file formats except xml,csv and json.
no error message is coming from solr
Sample XML File
<?xml version="1.0" encoding="UTF-8"?>
<content>just a test
</content>
Set the content type to "text/xml"
Add the following lines to your code:
OutputStreamWriter writer = new OutputStreamWriter(solrHttpURLConnection.getOutputStream());
writer.write(your_xml_file);
writer.flush();
Execute the request with this url http://localhost:7892/solr/myCore/update?literal.id=1234&commit=true
For json files use /update/json/docs
Please also check this documentation about uploading data with index handlers https://cwiki.apache.org/confluence/display/solr/Uploading+Data+with+Index+Handlers#UploadingDatawithIndexHandlers-XMLUpdateCommands

generate dynamic page in HTML from xml

I have several different XML files. I want to display them in HTML format using JSP file.
The structure of these files can be different. They may have different depth and tag's names.
I've read about XSLT but I found only examples with manually created xsl. (I can't do that because i will have many different xml files).
How can i produce my html?
EDIT:
Example of form:
<form name="Company">
<field name="name" required="true" type="inputText"/>
<field name="registrationDate" required="true" type="date"/>
<field name="isActive" required="false" type="boolean"/>
<field name="unregistrationDate" type="date"/>
<field name="type" type="comboBox">
<values>
<value>type1</value>
<value>type3</value>
<value>type3</value>
</values>
<defaultSelected>type2</defaultSelected>
</field>
</form>

Java Castor Unmarshal byte array

Good Day Community,
I have a question regarding castor unmarshaling in java. As mentioned I am using castor to unmarshal a webservice response that contains a byte array (byte[])
.. please refer to below:
public class ViewReportResponse {
private String reportId;
private byte[] report;
//getters and setters here ...
I have used castor before to unmarhsal webservice responses, but admittedly, the responses previously where always strings. The problem lyes with the byte array returned as I believe castor chokes on unmarshaling it.
My castor-mapping file is provided below:
<class name="com.domain.reporting.client.service.ViewReportResponse">
<map-to xml="viewReportResponse"
xsi:schemaLocation="http://domain.com/schemas/reportingService/generateReport"
ns-uri="http://domain.com/schemas/reportingService/generateReport"
ns-prefix="ns2" />
<field name="reportId">
<bind-xml name="reportId"
node="element"
type="string"/>
</field>
<field name="report">
<bind-xml name="report"
node="element"
type="bytes" />
</field>
I'm not sure what I am missing, but the message is received but fails at the point of unmarshaling.
I've attached a clip of the error below.
org.springframework.oxm.UnmarshallingFailureException: Castor unmarshalling exception; nested exception is org.exolab.castor.xml.MarshalException: unable to find FieldDescriptor for 'report' in ClassDescriptor of viewReportResponse.
Please any assistance is much appreciated.
Thank-you kindly
Solved:
The general problem is not the maping to and from the byte[]. The problem is related to the use of namespace in this case.
<field name="reportId">
<bind-xml name="ns:reportId" xmlns:ns="http://domain.com/schemas/reportingService/generateReport"
node="element"
type="string"/>
<field name="report">
<bind-xml name="ns:report" xmlns:ns="http://domain.com/schemas/reportingService/generateReport"
node="element"
type="bytes"/>
This post is now effectively solved and closed.

Why does j8583 Configparser fails with no default value to template fields?

I am using j8583 to build and parse ISO messages.
I have my template and parse config.xml in place, but when there are no default values specified to template fields it fails with NullPointerException.
Below is the template which fails with NullPointerException.
Field 3 doesn't have any default value.
<template type="0200">
<field num="3" type="NUMERIC" length="6"></field>
<field num="32" type="LLVAR">456</field>
<field num="35" type="LLVAR">4591700012340000=</field>
<field num="43" type="ALPHA" length="40">SOLABTEST TEST-3 DF MX</field>
<field num="49" type="ALPHA" length="3">484</field>
<field num="60" type="LLLVAR">B456PRO1+000</field>
<field num="61" type="LLLVAR"> 1234P</field>
<field num="100" type="LLVAR">999</field>
<field num="102" type="LLVAR">ABCD</field>
Well you're specifying field 3 to be a NUMERIC field of 6 digits and you didn't set any value. It's weird that you get NPE, should be NumberFormatException when it tries to parse the empty string.
Please file an issue on github.com/chochos/j8583 and I'll look into it (I'm the author of j8583).
In the meantime, try setting the value to 0. Or, if you don't need the field, just omit it from the template.

Data Import Handler in Solr

I am trying the Data Import Handler for MySQL Database.
I added the DIhandler in solrconfig.xml, created a data-config.xml according to my database scheme and also added a field in the schema.xml which was different. I am connecting with MySQL database
After i connect and I run the dataimport?command=full-import i get this response
"00C:\solr\conf\data-config.xmlfull-importidle1102011-03-05 15:01:04Indexing completed. Added/Updated: 0 documents. Deleted 0 documents.2011-03-05 15:01:042011-03-05 15:01:040:0:0.400This response format is experimental. It is likely to change in the future."
The xml files are in this http://pastebin.com/iKebKGSZ
<field column="manu" name="manu" />
<field column="id" name="id" />
<field column="weight" name="weight" />
<field column="price" name="price" />
<field column="popularity" name="popularity" />
<field column="instock" name="inStock" />
<field column="includes" name="includes" />
Are these fields also in your schema.xml?
I couldnt see them in the pastebin link. Make sure you have all fields in your schema as well.

Categories

Resources