default.xml:
<root id="test"><hello value="World">OK</hello></root>public class TestXml
{
public static Document getDocument(String fileName)
{
Document doc=null;
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse (new File (fileName));
doc.getDocumentElement ().normalize ();
} catch (SAXParseException err) {
System.out.println ("** Parsing error"
+ ", line " + err.getLineNumber ()
+ ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
} catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
} catch (Throwable t) {
t.printStackTrace ();
}
return doc;
}
public void init()
{
Document doc = getDocument("default.xml");
if(doc != null)
{
System.out.println("source:");
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Attribute.value:");
Element rootElement=doc.getDocumentElement();
Node rootNode=(Node)rootElement;
Node childNode = rootNode.getFirstChild();
NamedNodeMap nodeMap=childNode.getAttributes();
for(int j=0;j<nodeMap.getLength();j++)
{
String attrName=nodeMap.item(j).getNodeName();
if(attrName.equalsIgnoreCase("value"))
{
nodeMap.item(j).setNodeValue("Welcome");
}
}
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Change Attribute:");
Element elechild = (Element)childNode;
elechild.removeAttribute("value");
elechild.setAttribute("id","HELLO");
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Change textnode:");
Node textNode = childNode.getFirstChild();
textNode.setNodeValue("This is a test!");
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Add new node:");
Node newn = rootNode.cloneNode(false);
rootNode.appendChild(newn);
System.out.println(" "+doc.getDocumentElement().toString());
}
}
public static void main(String args[])
{
TestXml tx = new TestXml();
tx.init();
}
}
<root id="test"><hello value="World">OK</hello></root>public class TestXml
{
public static Document getDocument(String fileName)
{
Document doc=null;
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse (new File (fileName));
doc.getDocumentElement ().normalize ();
} catch (SAXParseException err) {
System.out.println ("** Parsing error"
+ ", line " + err.getLineNumber ()
+ ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
} catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
} catch (Throwable t) {
t.printStackTrace ();
}
return doc;
}
public void init()
{
Document doc = getDocument("default.xml");
if(doc != null)
{
System.out.println("source:");
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Attribute.value:");
Element rootElement=doc.getDocumentElement();
Node rootNode=(Node)rootElement;
Node childNode = rootNode.getFirstChild();
NamedNodeMap nodeMap=childNode.getAttributes();
for(int j=0;j<nodeMap.getLength();j++)
{
String attrName=nodeMap.item(j).getNodeName();
if(attrName.equalsIgnoreCase("value"))
{
nodeMap.item(j).setNodeValue("Welcome");
}
}
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Change Attribute:");
Element elechild = (Element)childNode;
elechild.removeAttribute("value");
elechild.setAttribute("id","HELLO");
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Change textnode:");
Node textNode = childNode.getFirstChild();
textNode.setNodeValue("This is a test!");
System.out.println(" "+doc.getDocumentElement().toString());
System.out.println("Add new node:");
Node newn = rootNode.cloneNode(false);
rootNode.appendChild(newn);
System.out.println(" "+doc.getDocumentElement().toString());
}
}
public static void main(String args[])
{
TestXml tx = new TestXml();
tx.init();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货