哪些方法可用于从Java中的文件返回有效和无效的XML数据?
|
我有以下数据应该是
XML:
<?xml version="1.0" encoding="UTF-8"?>
<Product>
<id>1</id>
<description>A new product</description>
<price>123.45</price>
</Product>
<Product>
<id>1</id>
<description>A new product</description>
<price>123.45</price>
</Product>
<ProductTTTTT>
<id>1</id>
<description>A new product</description>
<price>123.45</price>
</Product>
<Product>
<id>1</id>
<description>A new product</description>
<price>123.45</price>
</ProductAAAAAA>
所以,基本上我有多个根元素(产品)…… 关键是我正在尝试将这些数据转换为2个XML文档,1个用于有效节点,1个用于无效节点. 有效节点: <Product> ... </Product> 节点无效:< ProductTTTTT> …< / Product>和< Product> ……< / ProductAAAAAA> 然后我在想如何使用JAVA(而不是web)实现这一目标. >如果我没有错,使用XSD验证它将使整个文件无效,因此不是一个选项. 那么……我可以使用哪种方法来实现目标? (如果可能的话,请提供链接或代码) 解决方法
如果文件包含带有以“Product”开头的起始和结束标记的行,您可以:
>只要行以< Product或< / Product开头,就使用文件扫描程序将此文档拆分为单个部分 >如果成功,请将该对象添加到“良好”格式良好的XML文档列表中 >然后执行任何其他架构验证或有效性检查 >如果它抛出一个解析错误,请抓住它,并将该文本片段添加到需要清理或以其他方式处理的“坏”项列表中 一个让你入门的例子: package com.stackoverflow.questions.52012383;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class FileSplitter {
public static void parseFile(File file,String elementName)
throws ParserConfigurationException,IOException {
List<Document> good = new ArrayList<>();
List<String> bad = new ArrayList<>();
String start-tag = "<" + elementName;
String end-tag = "</" + elementName;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
StringBuffer buffer = new StringBuffer();
String line;
boolean append = false;
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if (line.startsWith(startTag)) {
append = true; //start accumulating content
} else if (line.startsWith(endTag)) {
append = false;
buffer.append(line);
//instead of the line above,you could hard-code the ending tag to compensate for bad data:
// buffer.append(endTag + ">");
try { // to parse as XML
builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(buffer.toString())));
good.add(document); // parsed successfully,add it to the good list
buffer.setLength(0); //reset the buffer to start a new XML doc
} catch (SAXException ex) {
bad.add(buffer.toString()); // something is wrong,not well-formed XML
}
}
if (append) { // accumulate content
buffer.append(line);
}
}
System.out.println("Good items: " + good.size() + " Bad items: " + bad.size());
//do stuff with the good/bad results...
}
}
public static void main(String args[])
throws ParserConfigurationException,IOException {
File file = new File("/tmp/test.xml");
parseFile(file,"Product");
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java.sql.SQLException:没有选择数据库 – 为什么?
- java使用pinyin4j实现汉语转拼音
- java – String [] a和String之间的区别…… a
- selenium+java破解极验滑动验证码的示例代码
- Spring Boot中整合Spring Security并自定义验证代码实例
- java – 带有Tomcat配置的NGINX
- Java数据结构及算法实例:选择排序 Selection Sort
- java – 为什么jsoup删除内联样式表?
- java – hibernate如何确保使用数据库中的最新数据更新二级
- Java GC概念:CMSInitiatingOccupancyFraction
