简体   繁体   English

解析具有相同父子标记的XML,然后最好使用SAX解析器将父标记的值链接到子标记

[英]Parse an XML having same parent-child tag and then link the value of parent tag to child tag using preferably SAX parser

I want to parse an XML having same parent-child tag and then link the value of parent tag to child tag using preferably SAX parser. 我想解析具有相同父子标记的XML,然后最好使用SAX解析器将父标记的值链接到子标记。

This is the XML file 这是XML文件

<?xml version="1.0" encoding="UTF-8"?>

<!-- Protocol header -->
<Protocol id="Diameter customer, country" spec="RFC3588" 
          name="Diameter" version="1.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:noNamespaceSchemaLocation="file:/$CLASSES/cmg/stdapp/diameter/validation/Diameter_addon_schema.xsd">
 <!-- ACR declaration: Start -->
  <Request name="Start">
    <Condition key="Accounting-Record-Type" value="2"/>
    <AVP name="Node-Id" defaultValue="MTAS"/>
    <AVP name="Session-Id"/>
    <AVP name="Origin-Host"/>

        <AVP name="Subscription-Id">
            <AVP name="Subscription-Id-Type"/>
            <AVP name="Subscription-Id-Data"/>
        </AVP>
        <AVP name="IMS-Information">
            <AVP name="Event-Type">
                <AVP name="SIP-Method"/>
            </AVP>
            <AVP name="Role-of-Node"/>
         </AVP> 

  </Request>
<!---->


</Protocol>

In this example a tag with name AVP has child tag with same name AVP . 在这个例子中使用名称的标签AVP具有相同名称的子标签AVP I want to get the value of attribute name and then associate the value of parent to that of child. 我想获取属性名称的值,然后将父级的值关联到子级的值。 I use SAX parser for this but I'm unable to distinguish between parent and child but there is no distinction parent and child tags. 我为此使用了SAX解析器,但是我无法区分父子标签,但没有父子标签的区别。

Java Code is Java代码是

public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException
{
    if (elementName.equalsIgnoreCase("AVP")) 
    {
        AVP_Tmp = new AVP();
        String nameValue = attributes.getValue("name");
         if (nameValue == null)
         {
             nameValue =attributes.getValue("value");
         }
         if (nameValue != null)
         {
             AVP_Tmp.set(nameValue,elementName,attributes);
         }
    }
}

@Override
public void endElement(String s, String s1, String element) throws SAXException 
{
    if (element.equals("AVP")) 
    {
        Object key = AVP_Tmp.tmpValue;
        Object value = AVP_Tmp.tmpValue;
        AVPL.put(key, value);                       
    }
}

The AVP_Tmp is a class whose set method is as follows: AVP_Tmp是一个类,其设置方法如下:

public void set(String nameValue, String qName, Attributes attrs)//, int k)
{
    int len = attrs.getLength();
    tmpValue=qName + "-->" + nameValue;
    List list = new ArrayList();
    for (int i = 0; i < len; i++)
    {
        list.add(attrs.getQName(i));
    }
    Collections.sort(list);
    for (int i = 0; i < len; i++)
    {
         name1[i]= (Object)list.get(i);
         value1[i]=(attrs.getValue((String) list.get(i)));
        tmpValue=tmpValue+ "\n" +name1[i]+"="+value1[i];
    }
}

I currently have output as: 我目前的输出为:

Node-Id
..
..
Subscription-Id
Subscription-Id-Type
IMS-Information
Event-Type
SIP-Method
..

I want the Output in format like: 我想要类似以下格式的输出:

Node-Id
..
..
..
Subscription-Id#Subscription-Id-Type
IMS-Information#Event-Type#SIP-Method
..

When I got your goals right, I would do it the way, that you build the "AVP-Structure" and afterwards extract the needed output. 当我实现了正确的目标时,我会按照以下方式进行操作:构建“ AVP结构”,然后提取所需的输出。

So when a new AVP starts it will look something like that (just in pseudo-code): 因此,当新的AVP启动时,它将看起来像这样(只是在伪代码中):

if (parent == null){
   avpTemp = new AVP();
   parent = avpTemp;
} else {avpTemp = new AVP(parent); }

After the parsing, you have got the hierarchy and build the structe/result as you want it. 解析之后,您将获得层次结构并根据需要构建结构/结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM