问题描述
我是解析字段的新手。我正在尝试编写解析器代码,但无法获取值包含&符号(&)
的特定标记的值。请帮我解决。
I am new to parsing field. I'm trying to write a parser code but unable to get the value with respect to a particular tag that value contains ampersand(&)
. Please help me to get the solution.
<system>
<u_id>10145</u_id>
<serial_no>1800015</serial_no>
<branch_name>B & P Infotech Ltd.</branch_name>
</system>
我试过这个java代码,但它没有给我正确的输出。
and I have tried with this java code, but it's not giving me proper output.
package com.satya.xmltest;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class SaxTest {
public static void main(String[] args) {
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SaxtestHandler handler=new SaxtestHandler();
try {
SAXParser parser = parserFactory.newSAXParser();
parser.parse("C:\\Users\\abc\\Desktop\\test.xml", handler);
} catch (Exception e) {
}
SystemTo systemTo=handler.systemTo;
System.out.println("Uid :"+systemTo.getUid());
System.out.println("serial number :"+systemTo.getSerialNumber());
System.out.println("name :"+systemTo.getName());
}
}
处理程序类
在这个类中,解析完成并将数据值设置为数据容器类。
Handler class
In this class the parsing is done and setting the data values to data container class.
package com.satya.xmltest;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SaxtestHandler extends DefaultHandler {
String content = "";
SystemTo systemTo=new SystemTo();
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
switch (qName) {
case "system":
System.out.println("inside company");
break;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
switch (qName) {
case "u_id":
systemTo.setUid(content);
break;
case "serial_no":
systemTo.setSerialNumber(content);
break;
case "branch_name":
systemTo.setName(content);
break;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
content = String.copyValueOf(ch, start, length).trim();
}
}
数据容器类
Data container class
package com.satya.xmltest;
public class SystemTo {
private String uid;
private String serialNumber;
private String name;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber) {
this.serialNumber = serialNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我的输出是:
My output is:
Uid: 10145
serial number: 1800015
name: null
但我需要:
But I need:
Uid: 10145
serial number: 1800015
name: B & P Infotech Ltd.
提前致谢。
推荐答案
XML中的某些字符不能以XML文档的文字形式出现,除非用作标记分隔符或在注释,处理指令或CDATA部分中使用。$
及其对应实体或要替换的数字引用:
There are some characters in XML that must not appear in their literal form in an XML document, except when used as markup delimiters or within a comment, a processing instruction, or a CDATA section.
List of characters and their corresponding entity or the numeric reference to replace :
Original Character XML entity replacement XML numeric replacement
" " "
< < <
> > >
& & &
' ' '
在解析之前,必须用XML替换上面的字符。
you must replace above character in XML before you parse it.
您可以使用对于非标记的文本构成文档的字符数据
You may use CDATA Section for text that is not markup constitutes the character data of the document
这篇关于无法解析包含特殊字符的值?使用sax解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!