我需要迭代xml缺少结束标记?

时间:2013-12-21 05:34:04

标签: java xml

 // Logic
     XMLInputFactory factory = XMLInputFactory.newInstance();
        Reader fileReader = new FileReader("1.xml");
        XMLEventReader reader = factory.createXMLEventReader(fileReader);

        while (reader.hasNext()) {
          XMLEvent event = reader.nextEvent();
          if (event.isStartElement()) {
            StartElement element = (StartElement) event;
            System.out.println("Start Element: " + element.getName());

            Iterator iterator = element.getAttributes();
            while (iterator.hasNext()) {
              Attribute attribute = (Attribute) iterator.next();
              QName name = attribute.getName();
              String value = attribute.getValue();
              System.out.println("Attribute name/value: " + name + "/" + value);
            }
          }
          if (event.isEndElement()) {
            EndElement element = (EndElement) event;
            System.out.println("End element:" + element.getName());
          }

          if (event.isCharacters()) {
            Characters characters = (Characters) event;
            System.out.println("Text: " + characters.getData());
          }
        }

此示例xml文件

    <Bank>
          <Account type="saving">
                <Id>1001</Id>
                <Name>Jack Robinson
                <Number>7898998
                <Amt>10000</Amt>
          </Account>
    </Bank>

2 个答案:

答案 0 :(得分:1)

在Java 7中:

Path xmlPath = Paths.get("1.xml");
byte[] bytes = Files.readAllBytes(xmpPath);
String xml = new String(bytes, StandardCharSets.ISO_8859_1);
xml = xml.replaceAll("^\\s*<(\w+)[^>]*>[^<]+$", "$0</$1>");
StringReader stringReader = new StringReader(xml);
XMLEventReader reader = factory.createXMLEventReader(stringReader);

这打算替换包含单个标记的行,后跟某些内容。

我已明确添加了编码。离开它,产生当前的平台编码,就像你的FileReader一样。 StandardCharSets,UTF_8通常适用于XML,您可以检查:除非<?xml ... encoding="..." ... ?>,否则它是默认值。


对于较旧的Java:

File file = new File("1.xml");
int size = (int) file.length();
byte[] bytes = new byte[size];
FileInputStrea in = new FileInputStream(file);
in.read(bytes);
in.close();
String xml = new String(bytes, "ISO-8859-1");

其余的是一样的;捕获的例外情况。

答案 1 :(得分:0)

<?php
libxml_use_internal_errors(true);
$sxe = simplexml_load_string("your XML code here!!");
if ($sxe === false) {
  echo "Failed loading XML\n";
  foreach(libxml_get_errors() as $error) {
     echo "\t", $error->message;
  }
}
?>

请参阅Jenkov的本教程 http://tutorials.jenkov.com/java-xml/stax-xmleventreader.html