新手求助:如何将Java Web应用与Tally ERP 9集成并实现数据操作
Hey there! Since you're new to integrating Tally ERP 9 with your Java Web app, let's break down how to sync data (specifically creating master data) step by step. Tally's primary way to interact with external systems is via its XML API—it's straightforward once you get the hang of the basics.
First, you need to enable Tally to accept requests from your Java app:
- Open Tally ERP 9, navigate to
Gateway of Tally > F12: Configure > Advanced Configuration > Security Control - Check the "Allow Remote Access" option, note down the port (default is 9000)
- Ensure the machine running Tally is reachable from your web app server (no firewall blocks on the specified port)
Tally expects XML requests that follow its schema to create/update master data. Below is a working example for creating a customer master (you can adapt this for other master types like items or vendors):
<ENVELOPE> <HEADER> <TALLYREQUEST>Import Data</TALLYREQUEST> </HEADER> <BODY> <IMPORTDATA> <REQUESTDESC> <REPORTNAME>All Masters</REPORTNAME> <STATICVARIABLES> <SVCURRENTCOMPANY>Your Company Name</SVCURRENTCOMPANY> </STATICVARIABLES> </REQUESTDESC> <REQUESTDATA> <TALLYMESSAGE xmlns:UDF="TallyUDF"> <CUSTOMER NAME="ABC Corp" ACTION="Create"> <PARENT>Customers</PARENT> <!-- Use the exact group name you created in Tally --> <LEDGERNAME>ABC Corp</LEDGERNAME> <ADDRESS>123 Business Park, Mumbai</ADDRESS> <PHONE>9876543210</PHONE> </CUSTOMER> </TALLYMESSAGE> </REQUESTDATA> </IMPORTDATA> </BODY> </ENVELOPE>
Important: Replace
Your Company Namewith your actual Tally company name, andCustomerswith the group name you manually created. Mismatched names will throw errors.
You can use Java's built-in HttpURLConnection or libraries like Apache HttpClient to send the XML request to Tally. Here's a simple, self-contained example:
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class TallyMasterSync { private static final String TALLY_BASE_URL = "http://<TALLY_MACHINE_IP>:9000"; // Replace with your Tally IP/port private static final String COMPANY_NAME = "Your Company Name"; public static boolean createCustomer(String customerName, String groupName, String address, String phone) throws Exception { // Build the XML request string String xmlPayload = buildCustomerXml(customerName, groupName, address, phone); // Send POST request to Tally URL url = new URL(TALLY_BASE_URL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/xml"); conn.setDoOutput(true); // Write XML to request body try (DataOutputStream dos = new DataOutputStream(conn.getOutputStream())) { dos.writeBytes(xmlPayload); dos.flush(); } // Check response from Tally int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { StringBuilder response = new StringBuilder(); String line; while ((line = br.readLine()) != null) { response.append(line); } // Verify if the request succeeded return response.toString().contains("<STATUS>Success</STATUS>"); } } else { System.out.println("Request failed with code: " + responseCode); return false; } } private static String buildCustomerXml(String customerName, String groupName, String address, String phone) { return "<ENVELOPE>" + "<HEADER><TALLYREQUEST>Import Data</TALLYREQUEST></HEADER>" + "<BODY>" + "<IMPORTDATA>" + "<REQUESTDESC>" + "<REPORTNAME>All Masters</REPORTNAME>" + "<STATICVARIABLES><SVCURRENTCOMPANY>" + COMPANY_NAME + "</SVCURRENTCOMPANY></STATICVARIABLES>" + "</REQUESTDESC>" + "<REQUESTDATA>" + "<TALLYMESSAGE xmlns:UDF=\"TallyUDF\">" + "<CUSTOMER NAME=\"" + customerName + "\" ACTION=\"Create\">" + "<PARENT>" + groupName + "</PARENT>" + "<LEDGERNAME>" + customerName + "</LEDGERNAME>" + "<ADDRESS>" + address + "</ADDRESS>" + "<PHONE>" + phone + "</PHONE>" + "</CUSTOMER>" + "</TALLYMESSAGE>" + "</REQUESTDATA>" + "</IMPORTDATA>" + "</BODY>" + "</ENVELOPE>"; } // Test the method from your web app's service layer public static void main(String[] args) { try { boolean success = createCustomer("XYZ Enterprises", "Customers", "456 Industrial Area, Delhi", "0123456789"); System.out.println("Customer creation status: " + (success ? "Success" : "Failed")); } catch (Exception e) { e.printStackTrace(); } } }
- Avoid Duplicates: If you try to create a master that already exists, Tally will return an error. To fix this:
- Send a pre-check request to fetch existing masters and verify if the record exists (use Tally's XML export API for this)
- Use
ACTION="Modify"instead ofCreateif you want to update existing records
- Mandatory Fields: Ensure all required fields are included (e.g.,
PARENTfor customers is non-negotiable). Tally will reject requests with missing mandatory data. - Encoding: Always use UTF-8 for your XML payloads to avoid character issues with non-English text.
- Error Handling: Even if the HTTP response is 200, parse the XML response thoroughly—look for
<ERROR>tags to catch Tally-specific issues.
To avoid duplicates, send this XML request to fetch all customer ledgers:
<ENVELOPE> <HEADER> <TALLYREQUEST>Export Data</TALLYREQUEST> </HEADER> <BODY> <EXPORTDATA> <REQUESTDESC> <REPORTNAME>List of Accounts</REPORTNAME> <STATICVARIABLES> <SVCURRENTCOMPANY>Your Company Name</SVCURRENTCOMPANY> </STATICVARIABLES> <TDL> <TDLMESSAGE> <REPORT NAME="List of Accounts" ISMODIFY="No"> <FORMS>List of Accounts</FORMS> </REPORT> <FORM NAME="List of Accounts" ISMODIFY="No"> <TOPPARTS>List of Accounts</TOPPARTS> <XMLTAG>LEDGER</XMLTAG> </FORM> <PART NAME="List of Accounts" ISMODIFY="No"> <LINES>List of Accounts</LINES> </PART> <LINE NAME="List of Accounts" ISMODIFY="No"> <FIELDS>LEDGERNAME</FIELDS> </LINE> <FIELD NAME="LEDGERNAME" ISMODIFY="No"> <TAG>NAME</TAG> </FIELD> </TDLMESSAGE> </TDL> </REQUESTDESC> </EXPORTDATA> </BODY> </ENVELOPE>
Parse the response to check if your target customer name already exists.
内容的提问来源于stack exchange,提问作者suresh




