『壹』 读取XML文件时出错!!
<?xml version="1.0"?>
<?xml version="1.0" encoding="gb2312"?>
我做了这样的改动才能支持中文,但是没有出现你说的找不到第五列的问题。两个xml都是可以正常读取的。
我都试过啦,把后面的行删除掉也可以再读取阿,当然,删除要符合xml的语法规则,不能把结构删错了。
『贰』 如何用java实现对xml文件的读取和写入以及保存
直接附源码import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;import org.dom4j.*;
import org.dom4j.io.XMLWriter;
public class Dom4jSample { public static void main(String[] args) {
Dom4jSample dom4jSample = new Dom4jSample();
Document document = dom4jSample.createDocument();
try{
dom4jSample.FileWrite(document);
Document documentStr = dom4jSample.StringToXML("<China>I Love!</China>");
dom4jSample.XMLWrite(documentStr);
Element legend = dom4jSample.FindElement(document);
System.out.println(legend.getText());
}
catch(Exception e)
{
}
}
/*
* Create a XML Document
*/
public Document createDocument()
{
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
Element author1 = root.addElement("Lynch");
author1.addAttribute("Age","25");
author1.addAttribute("Country","China");
author1.addText("I am great!");
Element author2 = root.addElement("Legend");
author2.addAttribute("Age","25");
author2.addAttribute("Country","China");
author2.addText("I am great!too!");
return document;
}
/*
* Create a XML document through String
*/
public Document StringToXML(String str) throws DocumentException
{
Document document = DocumentHelper.parseText(str);
return document;
}
public Element FindElement(Document document)
{
Element root = document.getRootElement();
Element legend = null;
for(Iterator i=root.elementIterator("legend");i.hasNext();)
{
legend = (Element)i.next();
}
return legend;
}
/*
* Write a XML file
*/
public void FileWrite(Document document) throws IOException
{
FileWriter out = new FileWriter("C:/Dom2jSample.xml");
document.write(out);
out.close();
}
/*
* Write a XML format file
*/
public void XMLWrite(Document document) throws IOException
{
XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStr.xml"));
writer.write(document);
writer.close();
}
}
『叁』 读取配置文件失败 怎样读取和处理XML的配置文件
Eclipse里在包下的配置文件(.propoties 和.xml等)编译之后会自动复制到编译后的文件夹中,也就是classes目录。因此代码可以通过路径加载到配置文件。然而,idea对这些配置的文件方式很明显和eclipse是不同的。在idea中有一个 Content Roots的概念。需要为每一个folder配置相应的Content Roots。Content Roots包括resources、source
『肆』 如何用MFC实现读取和修改XML文件中的数据
写
MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot;
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库");
return;
}
pDoc->raw_createElement((_bstr_t)(char*)"china",&xmlRoot);
pDoc->raw_appendChild(xmlRoot,NULL);
MSXML2::IXMLDOMElementPtr childNode;
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode);
childNode->put_text((_bstr_t)(char*)"WuHan");
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("8,000,000"));
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("10000"));
xmlRoot->appendChild(childNode);
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode);
childNode->put_text((_bstr_t)(char*)"ShangHai");
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("12,000,000"));
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("12000"));
xmlRoot->appendChild(childNode);
pDoc->save("e:\\he.xml");
读
MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库");
return;
}
pDoc->load("e:\\he.xml");
MSXML2::IXMLDOMElementPtr childNode;
childNode=(MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"));
MSXML2::DOMNodeType nodeType;
childNode->get_nodeType(&nodeType);
BSTR var;
childNode->get_nodeName(&var);
CString name=(char*)(_bstr_t)var;
VARIANT varVal;
childNode->get_nodeTypedValue(&varVal);
CString strValue=(char*)(_bstr_t)varVal;
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs=NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
childNode->get_attributes(&pAttrs);
long nCount;
pAttrs->get_length(&nCount);
for(int i=0;i<nCount;i++)
{
pAttrs->get_item(i,&pAttrItem);
CString strAttrName=(char*)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue=(char*)(_bstr_t)pAttrItem->nodeTypedValue;
AfxMessageBox(strAttrName+strAttrValue);
}
把上面的代码放到两个按钮下就可以了