『壹』 讀取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);
}
把上面的代碼放到兩個按鈕下就可以了