how to update xml file from another xml file dynamically?
Here is code what you want it's
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/two.xml");
DocumentTraversal traversal = (DocumentTraversal) doc;
Node a = doc.getDocumentElement();
NodeIterator iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
/**
* Logic for checking
**/
boolean flag=false;
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
Element e = (Element) n;
if ("int".equals(e.getTagName())) {
if(e.getAttribute("name").equals("linearLayout1")){
if(e.getAttribute("value").equals("8"))
flag=true;
}
}
}
/**
* Logic for reading one.xml and setting android:visibility="gone"
**/
docFactory = DocumentBuilderFactory.newInstance();
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.parse("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/one.xml");
traversal = (DocumentTraversal) doc;
a = doc.getDocumentElement();
iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
Element e = (Element) n;
if ("LinearLayout".equals(e.getTagName())) {
if(e.getAttribute("android:id").equals("@+id/linearLayout1")){
if(flag==true){
System.out.println(""+e.getAttribute("android:visibility"));
e.setAttribute("android:visibility", "gone");
}
}
}
}
/**
* Logic for rewriting one.xml
**/
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/home/riddhish/developerworkspace/SplitString/src/com/updatexmlwithjava/one.xml"));
iterator = traversal.createNodeIterator(a, NodeFilter.SHOW_ELEMENT, null, true);
doc = docBuilder.newDocument();
Element rootElement = doc.createElement("ScrollView");
doc.appendChild(rootElement);
for (Node n = iterator.nextNode(); n != null; n = iterator.nextNode()) {
rootElement.appendChild(doc.importNode(n, true));
}
transformer.transform(source, result);
dynamic recursive xslt to update an XML from another XML
To be honest, and as someone who believes that XSLT is very versatile, this looks like a job for a procedural language. With XSLT, you filter, so on every single node from the input document (jobs.xml), you have to look through the modification list and decide whether the current node fits all the criteria of any of the modification nodes. If nothing else, it will be pretty slow.
What XSLT cannot do is doing in-place updates. It always produces a new output from an input, although it can be used to simply copy part of the nodes from the input into the output. However, it seems much more "natural" to open the source document, and then iterate the modification list, searching one or more nodes fitting the criteria list, manipulating the values and adding/updating the "updated" attribute.
There is also a fair bit of intelligence required to interpret the criteria list. For example, /changes/modifications/modification[1]/identifiers/identifier[1] and identifier[2] have different paths, so one qualifies the parent element, while the other references the element to be modified. This all has to be coded into some logic that actually compares paths and does the matching, and doing that with XSLT will be very painful.
Fill data in XML from another XML file dynamically (Java)
Create XSLT and then something like that (very simple from oracle java tuts):
// ...
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
// ...
public class Stylizer {
// ...
public static void main (String argv[]) {
// ...
try {
File stylesheet = new File(argv[0]);
File datafile = new File(argv[1]);
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(datafile);
// ...
StreamSource stylesource = new StreamSource(stylesheet);
Transformer transformer = Factory.newTransformer(stylesource);
}
}
}
All informations and step-by-step guide to create XSLT and this sample app is here.
update xml file dynamically at runtime
Assuming you're using jdom-1.1.2.jar
Document doc = (Document) builder.build(YourFileName);
Element rootNode = doc.getRootElement();
List<Element> childrenNode = rootNode.getChildren();
for (Element child : childrenNode) {
System.out.println(child.getAttribute("value").getIntValue());
child.getAttribute("value").setValue("2");
}
// print updates to xml file with good formatting
XMLOutputter xmlOutput = new XMLOutputter();
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter(YourFileName));
Add an XML files content inside another XML document dynamically in Android using Eclipse
Your title is conceptually wrong, you don't one XML to another. Those XML are heavily crunched and pre-compiled during the compile time and don't exist as you see them in the final app.
Further, those XML are just representations for the system to build Views
and inside ViewGroups
which is a class that extends View
you can call .addView(view);
The include
xml code you use, is a good way to re-use static generated XML, but for dynamic generated stuff you need to do it via code.
I've notice you're using fragments stuff. So probably you're better use the Fragment route of dynamically adding/removing stuff
The code you created inside onNavigationItemSelected
is pretty much everything you need to do to dynamically change stuff around.
The fragment you're creating/instantiating will override onCreateView
to inflate a new View and return it. That new view will be inserted on android.R.id.content
(that is the View ID for your whole content) or for any ID that you specified in your XML.
hope it helps.
How to update xml files in java
Start by loading the XML file...
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DocumentBuilder b = f.newDocumentBuilder();
Document doc = b.parse(new File("Data.xml"));
Now, there are a few ways to do this, but simply, you can use the xpath API to find the nodes you want and update their content
XPath xPath = XPathFactory.newInstance().newXPath();
Node startDateNode = (Node) xPath.compile("/data/startdate").evaluate(doc, XPathConstants.NODE);
startDateNode.setTextContent("29/07/2015");
xPath = XPathFactory.newInstance().newXPath();
Node endDateNode = (Node) xPath.compile("/data/enddate").evaluate(doc, XPathConstants.NODE);
endDateNode.setTextContent("29/07/2015");
Then save the Document
back to the file...
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource domSource = new DOMSource(doc);
StreamResult sr = new StreamResult(new File("Data.xml"));
tf.transform(domSource, sr);
Related Topics
Apache Httpclient Digest Authentication
Google Gson Linkedtreemap Class Cast to Myclass
Double Tap: Zoom on Android Mapview
How to Wait for All Tasks in an Threadpoolexecutor to Finish Without Shutting Down the Executor
How to Create Button Dynamically in Android
Java: Need Some Way to Shorten This Code
Can't Install Rjava on Ubuntu System
Is There an Advantage to Running Jruby If You Don't Know Any Java
Python: How to Execute a Jar File Through a Python Script
Unhandled Exception Type Error
Android App Crashes After Sdk-Tools Update Version (Noclassdeffound, Tool Version 22)
How to Update Xml File from Another Xml File Dynamically