Java的XML解析器似乎认为我的XML文档在根元素之后没有很好地形成.但是我已经用几种工具对它进行了验证,他们都不同意.这可能是我的代码中的错误,而不是文档本身.我非常感谢你们能给我的任何帮助.
这是我的Java方法:
private void loadFromXMLFile(File f) throws ParserConfigurationException, IOException, SAXException {
File file = f;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc = null;
db = dbf.newDocumentBuilder();
doc = db.parse(file);
doc.getDocumentElement().normalize();
String desc = "";
String due = "";
String comment = "";
NodeList tasksList = doc.getElementsByTagName("task");
for (int i = 0; i tasksList.getLength(); i++) {
NodeList attributes = tasksList.item(i).getChildNodes();
for (int j = 0; i < attributes.getLength(); j++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName() == "description") { …Run Code Online (Sandbox Code Playgroud) 使用XmlDocument解析XML文件有什么选择,并且以后仍然保留错误消息的行信息?(顺便说一句,是否可以使用XML Deserialisation做同样的事情?)
选项似乎包括:
如果文档中存在属性,如何从XmlDocument中删除属性?请帮忙.我正在使用RemoveAttribute但是如何检查它是否存在.
root.RemoveAttribute(fieldName的);
谢谢..
<?xml version="1.0" standalone="yes" ?>
<Record1>
<Attribute1 Name="DataFieldName" Value="Pages" />
</Record1>
Run Code Online (Sandbox Code Playgroud)
我想删除名为"DataFieldName"的属性.
我想用Java创建XML.
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Run Code Online (Sandbox Code Playgroud)
但是Java会自动创建这样的声明
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
Run Code Online (Sandbox Code Playgroud)
我怎么能这样删除 encoding="UTF-8" standalone="no"它
<?xml version="1.0"?>
Run Code Online (Sandbox Code Playgroud)
谢谢!
当我尝试使用我总是得到一个访问冲突DocumentElement的XMLDocument.我XMLDocument根据一些文件的存在创建.
错误信息
项目project1.exe引发异常类EAccessViolation,消息'模块'地址0047B152中的访问冲突'project1.exe'.地址B1D59357'
我的代码
unit XMLBase;
interface
uses
SysUtils, xmldom, XMLIntf, XMLDoc, Forms;
type
TXMLbase = class
private
{ Private declarations }
public
XMLDocument1: TXMLDocument;
root: IXMLNode;
constructor Create;
end;
var
fn: string;
implementation
constructor TXMLbase.Create;
begin
fn := ChangeFileExt(Application.ExeName, '.xml');
XMLDocument1 := TXMLDocument.Create(nil);
XMLDocument1.Options := [doNodeAutoIndent];
XMLDocument1.Active := False;
//optional, is used to indent the Xml document
if FileExists(fn) then
begin
XMLDocument1.LoadFromFile(fn);
XMLDocument1.Active:= True;
root := XMLDocument1.DocumentElement; //<<--- Access Voilation
end
else
begin …Run Code Online (Sandbox Code Playgroud) 我有一个XmlNode代表以下xml的例子:
XmlNode xml.innerText =
<book>
<name><![CDATA[Harry Potter]]</name>
<author><![CDATA[J.K. Rolling]]</author>
</book>
Run Code Online (Sandbox Code Playgroud)
我想更改此节点,以便它包含以下内容:
XmlNode xml.innerText =
<book>
<name>Harry Potter</name>
<author>J.K. Rolling</author>
</book>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
谢谢!
我正在尝试使用XmlNamespaceManager将命名空间添加到XmlDocument.这是当前的xml:
<?xml version="1.0"?>
<kml>
<Document>
<Placemark>
</Placemark>
</Document>
</kml>
Run Code Online (Sandbox Code Playgroud)
我希望它转换为这个xml(使用XmlNamespaceManager):
<?xml version="1.0"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2"
xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Document>
<Placemark>
</Placemark>
</Document>
</kml>
Run Code Online (Sandbox Code Playgroud)
但我无法更改xml.这是代码,我知道它应该是一个简单的修复:
public void addXmlns()
{
string xml = @"<?xml version=""1.0""?>
<kml>
<Document>
<Placemark>
</Placemark>
</Document>
</kml>";
var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
//Add the namespaces
nsmgr.AddNamespace("", "http://www.opengis.net/kml/2.2");
nsmgr.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
nsmgr.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
string message;
message = xmldoc.InnerXml;
MessageBox.Show(message); // still shows the original xml
}
Run Code Online (Sandbox Code Playgroud)
先谢谢你
更新#1 - 试过这个,但它也不会改变XML: …
我在Windows商店中创建一个应用程序,并在创建的xml文件中写入xml时遇到问题.我在Windows应用商店应用中遵循了这个编辑XML文件,但它对我不起作用.
我想在按钮点击时在我的xml文件中写入这个xml.
这个东西的任何替代方式..
<drink>
<drinkImage>ck.png</drinkImage>
<drinkTitle>COKE</drinkTitle>
<drinkDescription>(1793-1844)</drinkDescription>
</drink>
Run Code Online (Sandbox Code Playgroud)
我当前的文件是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<drinks>
<drink>
<drinkImage>pepsi.png</drinkImage>
<drinkTitle>PEPSI</drinkTitle>
<drinkDescription>(1793-1844)</drinkDescription>
</drink>
**<here I Want above xml on button click>**
</drinks>
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
namespace DrinksApp
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class coke : Page
{
public coke()
{
this.InitializeComponent();
}
XmlDocument dom = new XmlDocument();
private void Button_Click(object sender, RoutedEventArgs …Run Code Online (Sandbox Code Playgroud) 我收到XML字符串中的消息; 我加载进去XmlDocument; 但是第二个节点每次都不同; 我在下面举例说明了三个例子:
<Message>
<Event1 Operation="Amended" Id="88888">Other XML Text</Event1>
</Message>
<Message>
<Event2 _Operation_="Cancelled" Id="9999999"> Other XML Text </Event2>
</Message>
<Message>
<Event3 Operation="Cancelled" Id="22222"> Other XML Text </Event3>
</Message>
Run Code Online (Sandbox Code Playgroud)
现在,我想找出第二个节点是Event1或者Event2还是Event3,也什么操作例如值"修订","取消","有序"?
在我被人们认为XML解析器不应该关心元素是空的还是自闭的之前,有一个原因是我不能允许自闭XML元素.原因是我实际上使用的是SGML而不是XML,而我正在使用的SGML DTD非常严格并且不允许它.
我所拥有的是数千个SGML文件,我需要运行XSLT.因此,我必须暂时将SGML转换为XML才能应用XSLT.然后我编写了一个方法,将它们转换回SGML(基本上只是用SGML声明替换XML声明并写回任何其他实体声明,如图形实体).
我的问题是,在转换回SGML之后,当我在SGML编辑器中打开文件时,文件不会解析,因为空元素已经自动关闭.
有没有人知道如何在使用XmlDocument时阻止这种情况发生?
将SGML转换为XML并再次返回的方法如下所示
//converts the SGML file to XML – it’s during this conversion that the
//empty elements get self-closed, i think
private XmlDocument convertToXML(TextReader reader)
{
// setup SgmlReader
Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
//sgmlReader.DocType = "HTML";
sgmlReader.WhitespaceHandling = WhitespaceHandling.All;
sgmlReader.CaseFolding = Sgml.CaseFolding.ToLower;
sgmlReader.InputStream = reader;
// create document
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.XmlResolver = null;
doc.Load(sgmlReader);
return doc;
}
// method to apply the XSLT stylesheet to the XML document
private …Run Code Online (Sandbox Code Playgroud)