markerData.unitName.replace(/<\/?.+?\/?>/g, '')
时间: 2023-07-19 18:31:29 浏览: 43
这个错误可能是因为 markerData.unitName 的值为 null 或 undefined 导致的。你可以在使用 `replace()` 方法前先判断一下 markerData.unitName 是否存在,例如:
```
if (markerData.unitName) {
markerData.unitName = markerData.unitName.replace(/<\/?.+?\/?>/g, '');
}
```
这样可以避免出现异常。另外,如果你能提供更详细的错误信息,我可以给你更准确的解答。
相关问题
replace(/</?.+?/?>/g, '')
这个正则表达式是用来匹配 HTML 标签的,但是可能存在一些问题。首先,在使用正则表达式时,最好使用正则表达式字面量,而不是字符串形式的正则表达式。其次,这个正则表达式可能会匹配到一些不符合预期的内容。
针对以上问题,建议改为以下正则表达式:
```
/<[^>]+>/g
```
这个正则表达式的含义是匹配所有的 HTML 标签,包括标签中的属性,但不包括标签中的内容。具体来说,`<[^>]+>` 这部分表示匹配尖括号中间的任何字符,`[^>]+` 表示匹配除了尖括号以外的任何字符,`g` 表示全局匹配。
使用这个正则表达式可以更准确地匹配 HTML 标签。完整的代码示例如下:
```
if (markerData.unitName) {
markerData.unitName = markerData.unitName.replace(/<[^>]+>/g, '');
}
```
<?xml version="1.0"?> <Results xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Code>0</Code> <Message>返回值内容</Message> <Data xsi:type="BillHeadResponse"> <InvtHead> <EportListNo/> <SeqNo/> </InvtHead> <ListDetail> <InvtList> <BillListFullEntity> <ListGNo>1</ListGNo> <GNo>813</GNo> </BillListFullEntity> <BillListFullEntity> <ListGNo>1</ListGNo> <GNo>813</GNo> </BillListFullEntity> </InvtList> <InvtDtList> <BillListFullEntity> <Unit>054</Unit> <Unit1>035</Unit1> <Unit2>054</Unit2> </BillListFullEntity> </InvtDtList> </ListDetail> <InOutList> <InOutStoreEntity> <SasStockNo>xxxxxxx</SasStockNo> <PoNo></PoNo> </InOutStoreEntity> </InOutList> </Data> </Results>java后端接收并解析以上数据
在Java后端中,您可以使用JAXP(Java API for XML Processing)来解析XML数据。以下是一些示例代码,可以帮助您开始解析XML数据:
```java
// 创建DocumentBuilderFactory对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
// 将XML数据解析为Document对象
Document document = builder.parse(new InputSource(new StringReader(xmlData)));
// 获取根元素
Element rootElement = document.getDocumentElement();
// 获取Code和Message元素的值
String code = rootElement.getElementsByTagName("Code").item(0).getTextContent();
String message = rootElement.getElementsByTagName("Message").item(0).getTextContent();
// 获取Data元素的类型
String dataType = rootElement.getElementsByTagName("Data").item(0).getAttributes()
.getNamedItem("xsi:type").getTextContent();
// 获取InvtHead元素的值
String eportListNo = rootElement.getElementsByTagName("EportListNo").item(0).getTextContent();
String seqNo = rootElement.getElementsByTagName("SeqNo").item(0).getTextContent();
// 获取ListGNo和GNo元素的值
NodeList invtList = rootElement.getElementsByTagName("InvtList");
for (int i = 0; i < invtList.getLength(); i++) {
Element invtListElement = (Element) invtList.item(i);
NodeList billListFullEntityList = invtListElement.getElementsByTagName("BillListFullEntity");
for (int j = 0; j < billListFullEntityList.getLength(); j++) {
Element billListFullEntityElement = (Element) billListFullEntityList.item(j);
String listGNo = billListFullEntityElement.getElementsByTagName("ListGNo")
.item(0).getTextContent();
String gNo = billListFullEntityElement.getElementsByTagName("GNo")
.item(0).getTextContent();
}
}
// 获取Unit、Unit1和Unit2元素的值
NodeList invtDtList = rootElement.getElementsByTagName("InvtDtList");
for (int i = 0; i < invtDtList.getLength(); i++) {
Element invtDtListElement = (Element) invtDtList.item(i);
NodeList billListFullEntityList = invtDtListElement.getElementsByTagName("BillListFullEntity");
for (int j = 0; j < billListFullEntityList.getLength(); j++) {
Element billListFullEntityElement = (Element) billListFullEntityList.item(j);
String unit = billListFullEntityElement.getElementsByTagName("Unit")
.item(0).getTextContent();
String unit1 = billListFullEntityElement.getElementsByTagName("Unit1")
.item(0).getTextContent();
String unit2 = billListFullEntityElement.getElementsByTagName("Unit2")
.item(0).getTextContent();
}
}
// 获取SasStockNo和PoNo元素的值
NodeList inOutList = rootElement.getElementsByTagName("InOutList");
for (int i = 0; i < inOutList.getLength(); i++) {
Element inOutListElement = (Element) inOutList.item(i);
NodeList inOutStoreEntityList = inOutListElement.getElementsByTagName("InOutStoreEntity");
for (int j = 0; j < inOutStoreEntityList.getLength(); j++) {
Element inOutStoreEntityElement = (Element) inOutStoreEntityList.item(j);
String sasStockNo = inOutStoreEntityElement.getElementsByTagName("SasStockNo")
.item(0).getTextContent();
String poNo = inOutStoreEntityElement.getElementsByTagName("PoNo")
.item(0).getTextContent();
}
}
```
请注意,此代码只是一个示例,您需要根据您的XML数据结构进行适当的修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)