问题
I want to know how I can update an XML file in real time. I had this file for example:
<?xml version="1.0" encoding="UTF-8"?>
<Cars>
<car make="Toyota" model="95" hp="78" price="120"/>
<car make="kia" model="03" hp="80" price="300"/>
</Cars>
What should I do to update the price value like this one? :
<?xml version="1.0" encoding="UTF-8"?>
<Cars>
<car make="Toyota" model="95" hp="78" price="50"/>
<car make="kia" model="03" hp="80" price="100"/>
</Cars>
I have searched the web but all I found was how to parse, and how to write the whole file using XmlSerializer, but not how to modify. Also I have found this in Java but I failed to implement it on Android because I'm very new to the android-xml world.
回答1:
OK after long day with search and tries I have reached my Goal using Java's DOM . To modify an XML file first instantiate these to handle the XML:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(context.openFileInput("MyFileName.xml")); // In My Case it's in the internal Storage
then make a NodeList for all "car" elements by :
NodeList nodeslist = doc.getElementsByTagName("car");
or all elements by replacing the car String with "*".
Now it well search in every node attributes till it fine the "price" value of KIA for example:
for(int i = 0 ; i < nodeslist.getLength() ; i ++){
Node node = nodeslist.item(i);
NamedNodeMap att = node.getAttributes();
int h = 0;
boolean isKIA= false;
while( h < att.getLength()) {
Node car= att.item(h);
if(car.getNodeValue().equals("kia"))
isKIA= true;
if(h == 3 && setSpeed) // When h=3 because the price is the third attribute
playerName.setNodeValue("100");
h += 1; // To get The Next Attribute.
}
}
OK Finally , Save the new File In the same location using Transformer like this :
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource dSource = new DOMSource(doc);
StreamResult result = new StreamResult(context.openFileOutput("MyFileName.xml", Context.MODE_PRIVATE)); // To save it in the Internal Storage
transformer.transform(dSource, result);
That's it :) . I hope this will helps .
回答2:
Underscore-java library can read, modify and write xml files. I am the maintainer of the project.
import com.github.underscore.lodash.U;
public class MyClass {
public static void main(String args[]) {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"
+ "<Cars>"
+ " <car make=\"Toyota\" model=\"95\" hp=\"78\" price=\"120\"/>"
+ " <car make=\"kia\" model=\"03\" hp=\"80\" price=\"300\"/>"
+ "</Cars>";
java.util.Map<String, Object> object = (java.util.Map<String, Object>) U.fromXml(xml);
U.set(object, "Cars.car[0].-price", "50");
U.set(object, "Cars.car[1].-price", "100");
System.out.println(U.toXml(object));
}
}
// <?xml version="1.0" encoding="UTF-8" standalone="no"?>
// <Cars>
// <car make="Toyota" model="95" hp="78" price="50"/>
// <car make="kia" model="03" hp="80" price="100"/>
// </Cars>
来源:https://stackoverflow.com/questions/36107697/update-modify-an-xml-file-in-android