I need to read XMl Data and store it in Text File, In the above code i am hard Coding getTagValue for all the Tag Names, If they are 4 tag names i can hardcode getTagValuebut now i had 200 tags and how can i read data into text file without hard coding getTagValue
When using DOM to parse the XML you must know the exact structure of the XML, so ther is no real way to avoid what your are doing.
If you have an XSD (if not you can write one), you can generate a Java object from it using some Xml binding framework like XmlBeans and then with one line you can parse the XML and start working with regular java object.
A sample code would be:
File xmlFile = new File("c:\employees.xml");
// Bind the instance to the generated XMLBeans types.
EmployeesDocument empDoc =
EmployeesDocument.Factory.parse(xmlFile);
// Get and print pieces of the XML instance.
Employees emps = empDoc.getEmployees();
Employee[] empArray = emps.getEmployeeArray();
for (int i = 0; i < empArray.length; i++)
{
System.out.println(empArray[i]);
}
Related
I want to convert dynamic xml file into a specific file format. i could able to parse the xml using jsoup parser but the problem is I want to parse the nested tags and put it into a for-loop.Is there any way to do it. Attaching the sample below for reference
Input XML(sample)
<lineComponents>
<invoiceComponents>
<invoiceComponent>
<type></type>
<name></name>
<amount>16.00</amount>
<taxPercentage>0.00</taxPercentage>
<taxAmount>0E-8</taxAmount>
</invoiceComponent>
</invoiceComponents>
<acctComponents>
<acctComponent>
<componentCode>BASE</componentCode>
<glAccountNr></glAccountNr>
<baseAmount>10.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>10.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
<acctComponent>
<componentCode></componentCode>
<glAccountNr></glAccountNr>
<baseAmount>3.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>3.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
<acctComponent>
<componentCode>DISC</componentCode>
<glAccountNr></glAccountNr>
<baseAmount>-2.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>-2.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
<acctComponent>
<componentCode>ARPIT</componentCode>
<glAccountNr></glAccountNr>
<baseAmount>5.00000</baseAmount>
<taxRate>0.00</taxRate>
<taxAmount>0.00000</taxAmount>
<totalAmount>5.00000</totalAmount>
<isVAT>No</isVAT>
</acctComponent>
</acctComponents>
</lineComponents>
Expected output:
for(OrderItem invoiceLineItem: orderLineWrp.invoiceLineItems){
Dom.XMLNode invoiceComponentNode = invoiceComponentsNode.addChildElement(EP_OrderConstant.invoiceComponent,null,null);
invoiceComponentNode.addChildElement(EP_OrderConstant.seqId,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_SeqId__c));
invoiceComponentNode.addChildElement(EP_OrderConstant.TYPE,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_ChargeType__c));
invoiceComponentNode.addChildElement(EP_OrderConstant.name,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Invoice_Name__c));
invoiceComponentNode.addChildElement(EP_OrderConstant.amount,null,null).addTextNode(getValueforNode(invoiceLineItem.UnitPrice)); //Value for amount
invoiceComponentNode.addChildElement(EP_OrderConstant.taxPercentage,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Tax_Percentage__c)); //Value for taxPercentage
invoiceComponentNode.addChildElement(EP_OrderConstant.taxAmount,null,null).addTextNode(getValueforNode(invoiceLineItem.EP_Tax_Amount_XML__c)); //Value for taxAmount
}
This Xml file is dynamic. Is there any way to handle dynamic XML file into a specific format like above?
Jsoup is rather for HTML parsing.
If you have XSD/DTD to your XML, you should use JAXB-generated classes and an unmarshaller to read it.
Otherwise you can use JAXP (DOMParser, if the file is small, and XPath, or event based SAXParser(however this is not so easy to use) for really large XML files).
i need a java code which makes an xsl style sheet that transforms an xml file to another xml file. but it should be dynamic.
i want to set the xsl:element names and path. and the java code should generate
me automaticly.
i have made one but if i want to change my type of xml, i need to add like 30 line code.
example from my code;
string xslelementstart = "<xsl:element name=\"" ;
string elementend="</xsl:element>";
string value="<xsl:value-of select=\"";
string name = "";(will be public and can be changed)
string path = "";(will be public and can be changed)
string end="\"\>";
string end2="\">";
if(path!="")
{
string xsl = xslelementstart+name+end2+"\n"+
value+path+end+"\n"
elementend
}
this is an example of my java code not the actual. im working with a big xml file. i want other xml files to be in my xml file format.but if i want to change my xml file (like adding another element) this code is not useful.as i said i should only set the values of my xml file and the java code should generates me. is it possible?
String concatenation is ugly, use Document Builder.
See the similar question: Create xslt files programmatically
What I need is point 3:
Take one xsd file "testField.xsd" and parse it using "com.codename1.xml.XMLParser"
alter this xsd file and generate result
now I want to save this xsd file with name "testValues.xsd"
What i have done so far :
(1). Take one xsd file "testField.xsd" and parse it using "com.codename1.xml.XMLParser"
InputStream is = Display.getInstance().getResourceAsStream(null,
"/testField.xsd"+fileName);
Element response = xp.parse(new InputStreamReader(is));
(2). alter this xsd file and generate result
for(FormData fromDataObj : listInputs){
response.getElementById(fromDataObj.getInputId()).setAttribute("value",fromDataObj.getInputValue());
}
(3). now I want to save this xsd file with name testValues.xsd
//want to save above response as "testValues.xsd"
Look at the XmlWriter class it has the ability to write Element's to XML.
I am having a problem with getting a name of a schema elements in java. I am creating a small xml editor which can load a xml schema and validate a xml file against xml schema. I want to parse a schema, get every elements name and then put it in my content assistant, so the user can see all the available elements.
I already read XSOM User's guide, but I didn't understand much...
Can someone help me to implement my addElementsFromSchema(File xsdfile) function, because I lost myself trying.
public static void addElementsFromSchema(File xsdfile){
}
It sounds like your primary need, at least for now, is to get the element names. You can get the element names with something like:
XSOMParser parser = new XSOMParser();
parser.parse(xsdfile);
XSSchemaSet schemas = parser.getResult();
Iterator<XSElementDecl> i = schemas.iterateElementDecls();
while (i.hasNext()) {
XSElementDecl element = i.next();
String name = element.getName();
// Add to editor
}
Showing element definitions is a lot more difficult, as element declarations in XML schemas can get quite complex.
I am working on an Android application that parses one or more XML feeds based on user preferences. Is it possible to parse (using SAX Parser) more than one XML feed at once by providing the parser with an array of URLs of my XML feeds?
If no, what would be an alternative way of listing the parsed items from different XML feeds in one list? An intuitive approach is to use java.io.SequenceInputStream to merge the two input streams. However, this throws a NullPointerException:
try {
URL urlOne = new URL("http://example.com/feedone.xml");
URL urlTwo = new URL("http://example.com/feedtwo.xml");
InputStream streamOne = urlOne.openStream();
InputStream streamTwo = urlTwo.openStream();
InputStream streamBoth = new SequenceInputStream(streamOne, streamTwo);
InputSource sourceBoth = new InputSource(streamBoth);
//Parsing
stream = xmlHandler.getStream();
}
catch (Exception error) {
error.printStackTrace();
}
List<Item> content = stream.getList();
return content;
The tactic of appending the streams before parsing is not likely to work well, as the appended XML will not be valid XML. As each XML input has its own root element, the appended XML will have multiple roots, which is not permitted in XML. Additionally it's likely to have multiple XML headers like
<?xml version="1.0" encoding="UTF-8"?>
which is also invalid.
While it's possible to preprocess the input to work around these issues, you're likely better off parsing them separately and dealing with getting the results combined later.
It's possible to make a SAX parser add the parsed elements to an existing list of elements. If you post code in your question showing how you're parsing a single file, we might be able to help figure out how to adjust it to your need for multiple inputs.