我在理解使用SAX解析XML结构时遇到了一些麻烦.假设有以下XML:
<root>
<element1>Value1</element1>
<element2>Value2</element2>
</root>
Run Code Online (Sandbox Code Playgroud)
和一个String变量myString.
只需使用方法startElement,endElement()和characters()就可以了.但我不明白我如何能够实现以下目标:
如果当前元素等于element1存储其价值value1在myString.据我所知,没有什么比如:
if (qName.equals("element1")) myString = qName.getValue();
Run Code Online (Sandbox Code Playgroud)
猜猜我只是觉得太复杂了:-)
罗伯特
目前我正在实现一个REST客户端,它将解析XML响应消息.稍后,它将在Android设备上运行.因此,存储器和处理速度是一个很大的问题.但是,一次只有一个XML响应,因此一次处理或保存多个XML文档不是问题.
据我所知,有三种方法可以使用Android SDK解析XML:
阅读这些不同的解析方法,我得到了SAX建议用于大型XML文件,因为它不会像DOM那样在内存中保存完整的树.
但是,我问自己在千字节,兆字节,......等方面有多大?是否有实用的尺寸,使用SAX或DOM无关紧要?
谢谢,
罗伯特
我的C#应用程序(.NET Framework 4.0)使用以下代码导入外部非托管DLL:
[DllImport("myDLL.dll"), EntryPoint="GetLastErrorText"]
private static extern IntPtr GetLastErrorText();
Run Code Online (Sandbox Code Playgroud)
不幸的是,第三方DLL中似乎存在一个错误。解决方法是,我需要卸载DLL,然后再重新加载。我怎样才能做到这一点?我看过几篇文章,但他们都谈论托管DLL。
我已经阅读了几篇关于MVVM模式的文章,教程和博客文章.但有一件事我不明白.采取三个"层":
据我了解MVVM,模型包含"原始"数据,例如Student类的情况下的名称和地址.视图模型向视图公开属性,该属性表示模型的数据.
视图模型中属性的示例
public string Name {
get { return model.Name; }
set { model.Name = value; }
}
Run Code Online (Sandbox Code Playgroud)
模型示例
private string name;
public string Name {
get { return name; }
set { name = value; }
}
Run Code Online (Sandbox Code Playgroud)
这可能听起来有点愚蠢,但这不会产生冗余吗?为什么我必须在模型和视图模型中保留名称?为什么不能完全处理视图模型上的名称?
在 Spring Boot 应用程序中,我用来WebClient调用对远程应用程序的 POST 请求。该方法目前如下所示:
// Class A
public void sendNotification(String notification) {
final WebClient webClient = WebClient.builder()
.defaultHeader(CONTENT_TYPE, APPLICATION_JSON_VALUE)
.build();
webClient.post()
.uri("http://localhost:9000/api")
.body(BodyInserters.fromValue(notification))
.retrieve()
.onStatus(HttpStatus::isError, clientResponse -> Mono.error(NotificationException::new))
.toBodilessEntity()
.block();
log.info("Notification delivered successfully");
}
// Class B
public void someOtherMethod() {
sendNotification("test");
}
Run Code Online (Sandbox Code Playgroud)
用例是:另一个类中的方法调用sendNotification并且应该处理任何错误,即任何非 2xx 状态或者甚至无法发送请求。
但我正在努力处理WebClient. 据我了解,以下行将捕获除 2xx/3xx 之外的任何 HTTP 状态,然后返回Mono.error带有NotificationException(自定义异常扩展Exception)的 a。
onStatus(HttpStatus::isError, clientResponse -> Mono.error(NotificationException::new))
但如何someOtherMethod()处理这种错误情况呢?它如何处理这个Mono.error?或者它实际上如何捕获NotificationExceptionifsendNotification甚至不将其放入签名中?
在我的C#应用程序中,我使用以下语句:
public void Write(XDocument outputXml, string outputFilename) {
outputXml.Save(outputFilename);
}
Run Code Online (Sandbox Code Playgroud)
如何找出Save方法可能抛出的异常?最好是在Visual Studio 2012中,或者在MSDN文档中.
XDocument.Save没有给出任何引用.它适用于其他方法,例如File.IO.Open.
我有一个模式文件,它没有定义任何目标命名空间,即它的定义如下所示:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<!--Elements, attributes, etc. -->
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
相应的XML如下所示:
<Documents p1:CRC="0" p1:Date="1900-01-01T01:01:01+01:00" p1:Name="Test" p1:Status="new" xmlns:p1="http://www.tempuri.org/pdms.xsd" xmlns="http://www.tempuri.org/pdms.xsd">
<p1:Document p1:Date="2010-12-23T07:59:45" p1:ErrorCode="0" p1:ErrorMessage="" p1:Number="TEST00001" p1:Status="new"/>
</Documents>
Run Code Online (Sandbox Code Playgroud)
使用例如Altova XMLSpy或Oxygen XML Editor对架构验证此XML失败.
但是,我在C#(.NET 4.0)中的验证不会失败.XML作为XDocument对象处理.如果我已经正确理解,那么XDocument.Validate()如果在模式中找不到命名空间,则执行松散验证.因此验证不会失败.但是,我该如何实施"严格"验证XDocument呢?
这就是我尝试验证XML的方法:
public static void ValidateXml(XDocument xml, string xsdFilename) {
XmlReaderSettings settings = new XmlReaderSettings();
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(string.empty, xsdFilename);
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
xml.Validate(schemaSet, ValidationCallback);
}
private static void ValidationCallback(object sender, ValidationEventArgs args) {
if (args.Severity …Run Code Online (Sandbox Code Playgroud) 我已经定义了以下类.
Document.cs
public class Document {
// ...
[XmlAttribute]
public string Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
DocumentOrder.cs
public class DocumentOrder {
// ...
[XmlAttribute]
public string Name { get; set; }
public List<Document> Documents { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
将此序列化为XML时,我得到:
<DocumentOrder Name="myname">
<Documents>
<Document Status="new"/>
// ...
</Documents>
</DocumentOrder>
Run Code Online (Sandbox Code Playgroud)
但是我想这样做,即Document成为孩子们的元素DocumentOrder.
<DocumentOrder Name="myname">
<Document Status="new"/>
<Document Status="new"/>
<Document Status="new"/>
// The document element has other attributes to distinguish...
</DocumentOrder>
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我需要与使用 iText 创建的 PDF 文档进行比较。我实际上设法比较了这些文件,但我被一个微小的差异绊倒了。
当在 Notepad++ 等编辑器中打开 PDF 时,我可以看到文件末尾有类似这样的内容:
/Root 1 0 R
/ID [<Some ID here> <Some other ID here>]
Run Code Online (Sandbox Code Playgroud)
正如我在这里发现的(What is the ID field in a pdf file?),这个元素属于“预告片”。
我可以使用 Apache PDFBox 访问和修改此“字段”吗?
我刚开始用Bulma设置我的博客原型.有一个footer部分有两个等分的列.
我希望这三个项目(Twitter,电子邮件等)在黄色区域中垂直居中.布尔玛有没有特别的课程?
(请参阅codepen.io上的完整示例.)
<footer class="footer" style="background-color: lightpink;">
<div class="columns">
<div class="column has-text-centered-touch" style="background-color: cyan;">
<p>Some copyright stuff...</p>
<p>Templated with <a href="https://bulma.io" target="_blank">Bulma</a>. Published with <a href="https://gohugo.io/" target="_blank">Hugo</a>.</p>
</div>
<div class="column has-text-right" style="background-color: yellow;">
<div class="level">
<div class="level-left"></div>
<div class="level-right">
<a class="level-item" href="#">Twitter Icon</a>
<a class="level-item" href="#">Email Icon</a>
<a class="level-item" href="#">LinkedIn Icon</a>
</div>
</div>
</div>
</div>
</footer>
Run Code Online (Sandbox Code Playgroud)