Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用sax解析器读取嵌套标记_Java_Xml_Sax - Fatal编程技术网

Java 使用sax解析器读取嵌套标记

Java 使用sax解析器读取嵌套标记,java,xml,sax,Java,Xml,Sax,我试图读取带有以下标记的xml文件,但sax解析器无法读取嵌套标记,如 <active-prod-ownership> <ActiveProdOwnership> <Product code="3N3" component="TRI_SCORE" orderNumber="1-77305469" /> </ActiveProdOwnership> </active-prod-ownership> 这是我正在使用的

我试图读取带有以下标记的xml文件,但sax解析器无法读取嵌套标记,如

<active-prod-ownership>
  <ActiveProdOwnership>
    <Product code="3N3" component="TRI_SCORE" orderNumber="1-77305469" />
  </ActiveProdOwnership>
</active-prod-ownership> 

这是我正在使用的代码

public class LoginConsumerResponseParser extends DefaultHandler {
 // ===========================================================
 // Fields
 // ===========================================================
 static String str="default";
 private boolean in_errorCode=false;
 private boolean in_Ack=false;
 private boolean in_activeProdOwnership= false;
 private boolean in_consumerId= false;
 private boolean in_consumerAccToken=false;


  public void startDocument() throws SAXException {
  Log.e("i am ","in start document");
 }


 public void endDocument() throws SAXException {
  // Nothing to do
  Log.e("doc read", " ends here");
 }

 /** Gets be called on opening tags like: 
  * <tag> 
  * Can provide attribute(s), when xml was like:
  * <tag attribute="attributeValue">*/

 public void startElement(String namespaceURI, String localName,
   String qName, Attributes atts) throws SAXException {
  if(localName.equals("ack")){
   in_Ack=true;
  }
  if(localName.equals("error-code")){
   in_errorCode=true;
  }
  if(localName.equals("active-prod-ownership")){
   Log.e("in", "active product ownership");
   in_activeProdOwnership=true;
  }
  if(localName.equals("consumer-id")){
   in_consumerId= true;
  }
  if(localName.equals("consumer-access-token"))
  {
   in_consumerAccToken= true;
  }
 }

 /** Gets be called on closing tags like: 
  * </tag> */

 public void endElement(String namespaceURI, String localName, String qName)
 throws SAXException {
  if(localName.equals("ack")){
   in_Ack=false;
  }
  if(localName.equals("error-code")){
   in_errorCode=false;
  }
  if(localName.equals("active-prod-ownership")){
   in_activeProdOwnership=false;
  }
  if(localName.equals("consumer-id")){
   in_consumerId= false;
  }
  if(localName.equals("consumer-access-token"))
  {
   in_consumerAccToken= false;
  }
 }

 /** Gets be called on the following structure: 
  * <tag>characters</tag> */

 public void characters(char ch[], int start, int length) {

  if(in_Ack){
  str= new String(ch,start,length);
  }
  if(str.equalsIgnoreCase("success")){
  if(in_consumerId){

  }
  if(in_consumerAccToken){

  }
  if(in_activeProdOwnership){
   str= new String(ch,start,length);
   Log.e("active prod",str);
  }
  }
 }
}
公共类LoginConsumerResponseParser扩展了DefaultHandler{
// ===========================================================
//田地
// ===========================================================
静态字符串str=“default”;
_errorCode中的私有布尔值=false;
_Ack中的私有布尔值=false;
_ActiveProdOwnery中的私有布尔值=false;
_consumerId中的私有布尔值=false;
consumerAccToken中的私有布尔值=false;
public void startDocument()引发异常{
Log.e(“我是”,“在启动文件中”);
}
public void endDocument()引发异常{
//无事可做
Log.e(“文件读取”,“到此结束”);
}
/**在开始标记上调用的获取,如:
*  
*当xml类似于以下内容时,可以提供属性:
* */
public void startElement(字符串namespaceURI、字符串localName、,
字符串qName、属性atts)引发异常{
if(localName.equals(“ack”)){
in_Ack=真;
}
if(localName.equals(“错误代码”)){
in_errorCode=true;
}
if(localName.equals(“活动产品所有权”)){
Log.e(“in”,“有效产品所有权”);
in_activeprodhownership=true;
}
if(localName.equals(“消费者id”)){
in_consumerId=true;
}
if(localName.equals(“使用者访问令牌”))
{
in_consumerAccToken=true;
}
}
/**在关闭标记上调用的控件,如:
*  */
public void endElement(字符串namespaceURI、字符串localName、字符串qName)
抛出SAX异常{
if(localName.equals(“ack”)){
in_Ack=false;
}
if(localName.equals(“错误代码”)){
in_errorCode=假;
}
if(localName.equals(“活动产品所有权”)){
in_activeprodhownership=false;
}
if(localName.equals(“消费者id”)){
in_consumerId=false;
}
if(localName.equals(“使用者访问令牌”))
{
in_consumerAccToken=false;
}
}
/**无法在以下结构上调用的获取:
*人物*/
公共无效字符(字符ch[],整数开始,整数长度){
如果(在确认中){
str=新字符串(ch、开始、长度);
}
if(str.equalsIgnoreCase(“success”)){
if(in_consumerId){
}
如果(在消费者知情同意书中){
}
如果(在所有权中){
str=新字符串(ch、开始、长度);
Log.e(“有效产品”,str);
}
}
}
}

但是当到达_activeProdOwnersip read only“中的标记时,我怀疑这就是问题所在:

new String(ch,start,length);
在这里,您将一个
char[]
传递给
String
构造函数,但是构造函数应该接受一个
byte[]
。最终结果是得到一个损坏的字符串

我建议您将
str
字段设置为
StringBuilder
,而不是
String
,然后使用以下内容:

builder.append(ch,start,length);

然后,每次调用
startElement()
时,您都需要清除
StringBuilder

XML文件中的标记与解析器不匹配。我认为您将标记与属性名称混淆了。下面是正确解析示例XML的代码:

public class LoginConsumerResponseParser extends DefaultHandler {
    public void startDocument() throws SAXException {
        System.out.println("startDocument()");
    }

    public void endDocument() throws SAXException {
        System.out.println("endDocument()");
    }

    public void startElement(String namespaceURI, String localName,
                             String qName, Attributes attrs) 
        throws SAXException {

        if (qName.equals("ActiveProdOwnership")) {
            inActiveProdOwnership = true;
        } else if (qName.equals("Product")) {
            if (!inActiveProdOwnership) {
                throw new SAXException("Product tag not expected here.");
            }
            int length = attrs.getLength();
            for (int i=0; i<length; i++) {
                String name = attrs.getQName(i);
                System.out.print(name + ": ");
                String value = attrs.getValue(i);
                System.out.println(value);            
            }
        }            
    }

    public void endElement(String namespaceURI, String localName, String qName)
        throws SAXException {

        if (localName.equals("ActiveProdOwnership"))
            inActiveProdOwnership = false;
    }

    public void characters(char ch[], int start, int length) {
    }

    public static void main(String args[]) throws Exception {
        String xmlFile = args[0];
        File file = new File(xmlFile);
        if (file.exists()) {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            DefaultHandler handler = new Test();
            parser.parse(xmlFile, handler);
        }
        else {
            System.out.println("File not found!");
        }
    }

    private boolean inActiveProdOwnership = false;
}

请发布一个您期望的输出示例。由于您的示例不包含纯文本数据,只包含带有属性的嵌套标记,我认为您可能期望一些永远不会发生的事情,但需要一些上下文来确保。
startDocument()
code: 3N3
component: TRI_SCORE
orderNumber: 1-77305469
endDocument()